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