Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy method not firing to destroy nested resource in complex form

I am running Rails 3.0.3 with Paperclip 2.3.8. I have two models, call them "Post" and "Image". Image is an object containing a Paperclip image file to dynamically add images to Posts. Image belongs to Post, and Post has many Images.

Post Model:

class Post < ActiveRecord::Base
 has_many :images
 accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:image].blank? },  :allow_destroy => true
end

Image Model:

class Image < ActiveRecord::Base
 belongs_to :post
 has_attached_file :image,
      :styles => {
      :thumb=> "100x100#",
      :small  => "300x300>" }
end

I have a form in which I would like to dynamically add and remove images. If the image is a new image being uploaded I would like to show a file_field; otherwise if the image already exists I would like to display the image and a remove link. I followed Ryan Bates' Railscasts on Nested Forms. Adding images dynamically is working, but the call to _destory is not firing. The post contains "_destroy"=>"1", but after the HTTP Post event the image with id 7 still exists. Here is an excerpt from the HTTP Post:

"images_attributes"=>{
  "0"=>{"id"=>"7", "_destroy"=>"1"}, 
  "1"=>{"id"=>"8", "_destroy"=>"false"}, 
  "2"=>{"id"=>"9", "_destroy"=>"false"}, 
  "3"=>{"id"=>"10", "_destroy"=>"false"}}

Here is what the form looks like:

<%= form_for @post, :html => { :multipart => true } do |f| %>
 <%= f.label :images, 'Images' %><br />
 <div class="field">
  <%= f.fields_for :images do |s| %>
   <%= render 'image_fields', :f => s %>
  <% end%>
 </div> 
 <div class="field">
  <%= link_to_add_fields "Add Image", f, :images %>
 </div> 
 <div class="actions">
  <%= f.submit %>
 </div>
<% end %>

_image_fields:

<div class="fields">
 <% if !f.object.new_record? %>
  <%= image_tag f.object.image.url %>
 <% else %>
  &nbsp; <%= f.file_field :image%>
 <% end %>
 <%= link_to_remove_fields "remove", f %>
</div>

Javascript Functions:

function remove_fields(link){
 $(link).previous("input[type=hidden]").value = "1";
 $(link).up(".fields").hide();
}

function add_fields(link, association, content) {
 var new_id = new Date().getTime();
 var regexp = new RegExp("new_" + association, "g")
 $(link).up().insert({
  before: content.replace(regexp, new_id)
 });
}

Helper Functions:

def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) +
    link_to_function(name, "remove_fields(this)")
end

def link_to_add_fields(name, f, association)
 new_object = f.object.class.reflect_on_association(association).klass.new
 fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
  render(association.to_s.singularize + "_fields", :f => builder)
 end
 link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end

As I mentioned above I am able to add images. Clicking the "Add Image" link adds a file field to the form and I can add multiple images successfully. Clicking the "remove" link by an image removes the link and the image from the view - but as I said it is not deleted on Post event.

Thank you in advanced; I appreciate anyone taking time to help me find a solution.

Edit:

--Solution--

In the Post Controller added ".includes(:images)" to the find in the update method:

def update
 @post = Post.includes(:images).find(params[:id])

 respond_to do |format|
   if @post.update_attributes(params[:post])
     format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
     format.xml  { head :ok }
   else
     format.html { render :action => "edit" }
     format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
   end
 end
end
like image 927
dmlogs Avatar asked Feb 17 '11 00:02

dmlogs


1 Answers

I've got a very similar issue. I was able to 'sort of' resolve it by removing the :reject_if clause in the model.

 accepts_nested_attributes_for :images, :allow_destroy => true

Once I removed it, I am able to remove my fields with no problem. The obvious downside to this is a user can now add a blank / invalid image and the model won't reject it.

EDIT:

It looks like this is actually a bug with Rails 3.0.4 in regards to how the accepts_nested_attributes_for handles the :allow_destroy and :reject_if. See the answer here for another workaround: Paperclip problem for accepts_nested_attributes_for and reject_if

like image 69
mintshows Avatar answered Nov 04 '22 18:11

mintshows