sparkle
sparkle

Reputation: 7390

Two nested form objects

I have two nested objects (Attachment inside Widget, Widget inside Lesson).

Lesson > Widgets > Attachments

Lesson > Widgets > Links

Everything is fine, but if I click on "Remove attachment", and then I click on "Update Lesson", nested attachments in the widget are not removed. Is that because I click on "Update Lesson", instead of "Updated Widget" ?

enter image description here

Lesson

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

Attachment

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

   mount_uploader :file, AttachmentUploader

Link

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

Lesson (Edit Form)

<%= 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>

Upvotes: 0

Views: 174

Answers (2)

Nicholas.V
Nicholas.V

Reputation: 1936

Assuming you are using rails 4, have you checked strong parameters to ensure :_destroy is specified for attachment_attributes?

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

Tip: You can check your development log output when you submit the form to see if there are any unpermitted parameters.

Upvotes: 0

kendotwill
kendotwill

Reputation: 2020

Change this line

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

to

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

Upvotes: 3

Related Questions