13

I'm trying to create a simple app with the following models: categories --[has_many]--> questions --[has_many]--> answers

I have the following code for creating categories + questions(categories/_form.haml.html):

= simple_form_for(@category) do |f|
  = f.error_notification
  = f.input :title, label: "Category title: "
  = f.simple_fields_for :questions, @category.questions.build do |q|
    = q.input :content, label: "Question content: "
  = f.button :submit

And I'm using all the same code for creating questions + answers(questions/_form.haml.html). I have all the relations, strong parameters, nested attrs and controllers configured, it works just fine for me.

Two questions:

  1. How to create multiple questions in categories/_form.haml.html?

  2. How to create category + multiple questions + multiple answers per each question at once(in categories/_form.haml.html)?

I've spent a few hours trying to figure out how to accomplish the second one and all the info I was able to find is related to Rails 3.0 and form_for. None of them worked for me.

The most straightforward solution here should be something like:

= simple_form_for(@category) do |f|
  = f.error_notification
  = f.input :title, label: "Category title: "
  = f.simple_fields_for :questions, @category.questions.build do |q|
    = q.input :content, label: "Question content: "
    = q.simple_fields_for :answers, q.questions.build do |a|
      = a.input :content, label: "Answer content"
  = f.button :submit

But it gives me

undefined method `questions' for #<SimpleForm::FormBuilder:

What am I missing here?

2 Answers 2

12

You got it wrong here: = q.simple_fields_for :answers, q.questions.build do |a| You are calling questions method on builder object q instead of a model object. Probably You want this:

= q.simple_fields_for :answers, q.object.questions.build
Sign up to request clarification or add additional context in comments.

Comments

3

i am not going to answer the first two questions as i think they are explained here in depth: http://railscasts.com/episodes/196-nested-model-form-part-1

i just want to give you some hints about the error. you really have to learn how to read error-messages and stacktraces if you want to become a professional.

so here is a detailed explanation of the error that states undefined methodquestions' for #

first of all, it is very important to provide complete stacktraces. that is because they include line numbers. line number are important when resolving issues.

i guess that the line in question here is = q.simple_fields_for :answers, q.questions.build do |a|

if you look at the message, it says that the object q is of type FormBuilder. this is the object that rails instantiates when you call form_for or fields_for. when you use SimpleForm, it's also in simple_form_for and simple_fields_for, but an extended version (most often called a decorated version).

this object q does not have a method question and never will! i assume that you want to access the underlying object that the fields_for method wraps. you can access this via q.object (see this post for more infos Rails - Using form_for and fields_for, how do you access the sub-object while in the fields_for block?).

in your case i also assume a mix of answers versus questions. i think that this should be q.simple_fields_for :answers, q.object.answers.build instead of q.simple_fields_for :answers, q.questions.build.

1 Comment

Thanks phoet. Mix between answers and question was a copy&paste mistake. And I've found stackoverflow.com/questions/5038341/… before but read it too quickly. I was sure I'm doing it incorrectly by trying to call "questions" or "answers" method on FromBuilder but I had no idea I could access the underlying object by just calling object method on it. Thanks for taking time for such a detailed answer. And I'm still not sure I should use railcasts episodes for Rails 3.0 since it really differs

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.