两个嵌套的表单对象

Two nested form objects

我有两个嵌套对象(Widget 内的附件,Lesson 内的 Widget)。

课程 > 小工具 > 附件

课程 > 小工具 > Links

一切正常,但如果我单击 "Remove attachment",然后单击 "Update Lesson",则不会删除小部件中的嵌套附件。那是因为我点击了 "Update Lesson" 而不是 "Updated Widget" 吗?

课程

class Lesson < ActiveRecord::Base
  has_many :widgets, as: :widgetable, autosave: true
  accepts_nested_attributes_for :widgets, allow_destroy: true

小组件

 class Widget < ActiveRecord::Base
  belongs_to :widgetable, polymorphic: true

  has_many :attachments, as: :attachable, autosave: true
  accepts_nested_attributes_for :attachments, allow_destroy: true 
  has_many :links, as: :linkable, autosave: true
  accepts_nested_attributes_for :links, allow_destroy: true

附件

class Attachment < ActiveRecord::Base
    belongs_to :attachable, polymorphic: true

   mount_uploader :file, AttachmentUploader

Link

class Link < ActiveRecord::Base
  belongs_to :linkable, polymorphic: true

课程(编辑表格)

<%= f.fields_for :widgets do |w| %>
                <div class="row">
                  <div class="col-xs-8">
                    <h2>Name</h2>
                    <%=w.text_field :name, placeholder: "Name of widget", class: "form-control"%>

                    <%= w.fields_for :attachments do |h| %>
                      <h4>Attachments</h4>
                      <div class="row">
                        <div class="col-sm-6">
                          <%=h.text_field :name, placeholder: "Title", class: "form-control"%>
                        </div>
                        <div class="col-sm-6">
                          <%=h.file_field :file, class: "form-control"%>
                        </div>
                      </div>
                      <div class="row">
                        <div class="col-sm-12">
                          <br />
                          <%=h.text_area :description, placeholder: "Write a description", class: "form-control autogrow"%>
                        </div>
                      </div>
                       <%= w.link_to_remove "Remove attachment", class: "text-right"%>
                      <hr /><br />
                    <% end %>

                    <%= w.fields_for :links do |l| %>
                      <h4>Links</h4>
                       <%=l.text_field :title, placeholder: "Title", class: "form-control"%>
                       <%=l.text_field :description, placeholder: "Description", class: "form-control"%>
                       <%=l.text_field :url, placeholder: "http://", class: "form-control"%>
                       <%= l.link_to_remove "Remove Link", class: "text-right"%>
                      <hr /><br />
                    <% end %>


                    <p>
                      <%=w.link_to_add "Add attachment", :attachments, class: "btn btn-sm btn-success" %>
                      <%=w.link_to_add "Add Link", :links, class: "btn btn-sm btn-success" %>
                    </p>
                  </div>
                  <div class="col-xs-4 text-right">
                    <%= w.link_to_remove "Remove widget", class: "btn btn-sm btn-danger" %>
                  </div>
                </div>

               <hr />
               <p></p>

              <% end %>
              <p><%= f.link_to_add "Add widget", :widgets, class: "btn btn-sm btn-success pull-right" %></p>

更改此行

<%= w.link_to_remove "Remove attachment", class: "text-right"%>

<%= h.link_to_remove "Remove attachment", class: "text-right"%>

假设您使用的是 rails 4,您是否检查过强参数以确保为 attachment_attributes 指定了 :_destroy?

https://github.com/ryanb/nested_form#strong-parameters

提示:您可以在提交表单时检查您的开发日志输出,看看是否有任何不允许的参数。