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:
How to create multiple questions in categories/_form.haml.html?
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?
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
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 method
questions' 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
.