Rails 3 Nested Model Form, 2 levels deep using accepts_nested_attributes_for

时间秒杀一切 提交于 2019-11-30 05:05:34

I think you've got the form variables slightly mixed up. It should be:

= form_for @question, :html => {:multipart => true} do |f|

  = f.label :text, "Question Text:"
  = f.text_area :text, :rows => 7

  %br
  %br

  =f.fields_for :answer, do |af|
    = af.label :body, "Answer Text:"
    = af.text_area :body, :rows => 7

    %br
    %br

    = af.fields_for :image do |img_form|
      = img_form.label :title, "Image Title:"
      = img_form.text_field :title

      %br

      = img_form.label :file, "Image File:"
      = img_form.file_field :file

      %br

      = img_form.label :caption, "Image Caption:"
      = img_form.text_area :caption, :rows => 7

  = hidden_field_tag("case_id", value = @case_id)

  = f.submit

Notice how form_for ... do |f| spawns f.fields_for ... do |af|, which in turns spawns af.fields_for ... do |img_form|.

The key is the second fields_for. It should be af.fields_for :image do |img_form| rather than f.fields_for :image do |img_form|.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!