我正在尝试构建嵌套表单,但 question_fields 的嵌套表单未在浏览器中呈现。该表单有一个称为答案的嵌套表单,也不会呈现
这是嵌套表单,_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'
这是_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
这是相关的投票控制器,新建和创建操作
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
关于为什么这可能不会呈现的任何想法?提前感谢您的提示!
更新,一个新问题
在创建带有相关问题和答案的投票后,在查询数据库后,我看到外键没有持久化并且关联丢失了。我必须以某种方式在这里使用隐藏字段吗?