Reputation: 742
I'm trying to build a nested form, but the nested form of question_fields isn't rendering in the browser. That form has a nested form called answers, also not rendering
Here's the nested form, _createpoll.html.haml
= form_for Poll.new, :class=>'create-poll-form', :remote => true do |f|
= f.text_field :title, :autofocus => true, :placeholder => "Poll Title"
= f.text_field :description, :placeholder => 'Description'
/ Required accepts_nested_attributes_for :questions in the polls model.
= f.fields_for :questions do |builder|
= render "questions/question_fields", :f => builder
= f.submit "Create Poll", :class => 'btn btn-danger'
Here's the _questions_fields.html.haml:
%p
= f.label :content, "Question"
= f.text_area :content
= f.check_box :_destroy
= f.label :_destroy, "Remove Question"
%p
= f.fields_for :answers do |builder|
= render "answers/answer_fields", :f => builder
Here's the related Polls Controller, new and create actions
def create
@poll = Poll.create(params[:poll])
end
def new
@poll = Poll.new
1.times do
question = @poll.questions.build
2.times {question.answers.build}
end
end
Any ideas on why this might not be rendering? Thanks in advance for the tips!!
Update, a new question
After creating the poll with its associated questions and answers, after querying the database, I see that that the foreign keys aren't persisted and the association is lost. Do I have to use hidden fields here somehow?
Upvotes: 0
Views: 60
Reputation: 742
Silly oversight. Poll.new has to be @poll... whoops.
Updating Answer.
This form was was rendered by a button on the user's dashboard, "Create Poll," routing through the controller's new action.
As controller's new action instantiates the new poll, I was being redundant when instantiating another poll in the form for
. By switching this to the newly created instance variable @poll
, the form rendered. Also, :content
threw a no method error, but that's because it wasn't in the attr_accessible
of the questions or answers models.
Upvotes: 1