Search code examples
ruby-on-railsrubyruby-on-rails-4cocoon-gem

cocoon gem can't remove nested field image


I have set up the cocoon gem correctly and everything is working fine, I can upload images as required but when I click remove, it does not seem to remove the image. Here is my code:

_image_fields.html.erb

<div class="nested-fields">
  <%= f.text_area :description %>
  <%= f.file_field :picture %>
  <%= link_to_remove_association "remove", f %>
</div>

here is my form:

<div class="imagefield">
  <h2>Images</h2>
    <h4>upload images</h4>
      <hr>
       <div class="customgap">
         <div class="images">
           <%= f.fields_for :images do |image| %>
             <%= render 'image_fields', f: image  %>
           <% end %>
          <div class="links">
            <%= link_to_add_association 'add image', f, :images %>
          </div>
         </div>
       </div>
     </div>

in controller:

images_attributes: [:id, :description, :picture, :_destroy]

Image model

class Image < ActiveRecord::Base

  has_attached_file :picture, styles: { medium: "300x300>", thumb: "100x100>" }
  validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/
end

Solution

  • I had to :allow_destroy => true in the controller

    accepts_nested_attributes_for :images, :allow_destroy => true