如何在Rails表单中显示嵌入式关系的空字段表单?

时间:2014-03-17 21:12:02

标签: ruby-on-rails ruby-on-rails-3

我有一个有很多答案的问题,我想为答案显示一个空白输入字段:

- @q = Question.new
= form_for @q do |f|
  = f.label :title
  = f.text_field :title
  = f.label :answers
  = f.fields_for :answers, @q.answers do |fields|
    = fields.text_field :title
  = f.submit "Update"

但目前只有我的问题标题空白输入显示,但没有答案空白输入???

enter image description here

1 个答案:

答案 0 :(得分:1)

这是设置具有嵌套字段的表单的正确方法(遵循约定):

#controller
@question = Question.new

#view
= form_for @question do |f|
  = f.label :title
  = f.text_field :title
  = f.label :answers
  = f.fields_for @question.answers.new(title: 'default title') do |answer_fields|
    = answer_fields.text_field :title
  = f.submit "Update"