【问题标题】:Two nested form objects两个嵌套的表单对象
【发布时间】:2015-02-27 21:55:47
【问题描述】:

我有两个嵌套对象(小部件内的附件,课程内的小部件)。

课程 > 小部件 > 附件

课程 > 小部件 > 链接

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

课程

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

链接

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>

【问题讨论】:

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


    【解决方案1】:

    改变这一行

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        相关资源
        最近更新 更多