1

I have a form where the user signs up and creates an Account, an User and a Website.

def new
  @account = Account.new
  @account.users.build
  @account.websites.build
  ...
end

def create
  @account = Account.new(params[:account])
  ...

Everything works fine. Now, I want to create a default Page with Page.title = "homepage" and Page.body = "".

How can I do that? I tried different options and it doesn't work. For example, I do this @account.websites.pages.build and I get this undefined method pages for []:ActiveRecord::Relation.

2
  • 1
    try @account.websites.build.pages.build Commented Sep 27, 2011 at 19:57
  • Tried but I get undefined method title for nil class @account.websites[0].pages[0].title = "homepage" Commented Sep 27, 2011 at 20:13

1 Answer 1

1

The collection returned by @account.websites is an array, rails can't intuit which member of the collection you're trying to create an associated object on... You need to specify which website you want to build a page for, ie

@account.websites.first.pages.build
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.