两个嵌套的表单对象

时间:2015-02-27 21:55:47

标签: ruby-on-rails ruby-on-rails-4 nested nested-forms nested-attributes

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

课程>小部件>附件

课程>小部件>链接

一切都很好,但如果我点击“删除附件”,然后点击“更新课程”,则不会删除窗口小部件中的嵌套附件。那是因为我点击“更新课程”,而不是“更新的小工具”?

enter image description here

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

的widget

 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

链接

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>

2 个答案:

答案 0 :(得分:3)

更改此行

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

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

答案 1 :(得分:0)

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

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

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