Creating multiple nested forms using simple_form and rails 4
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
.