Destroy method does not shoot to destroy an embedded resource in complex form

I am running Rails 3.0.3 using Paperclip 2.3.8. I have two models, name them "Mail" and "Image". An image is an object containing a Paperclip image file for dynamically adding images to messages. The 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

, . - , file_field; , , . RailsBates Ryan Bates . , _destory . "_destroy" = > "1", HTTP Post 7 . HTTP-:

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

:

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

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)
 });
}

:

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

, . " " , . "" - , , Post.

; , - , .

Edit:

- -

Post Controller ".includes(: images)" :

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
+5
1

. "" , : reject_if .

 accepts_nested_attributes_for :images, :allow_destroy => true

, . , / , .

:

, Rails 3.0.4 , accepts_nested_attributes_for : allow_destroy : reject_if. . : accepts_nested_attributes_for reject_if

+9

All Articles