销毁方法不会以复杂的形式销毁嵌套资源

时间:2011-02-17 00:18:55

标签: ruby-on-rails ruby-on-rails-3 paperclip nested-forms nested-attributes

我正在使用Paperclip 2.3.8运行Rails 3.0.3。我有两个模型,称它们为“Post”和“Image”。 Image是一个包含Paperclip图像文件的对象,用于动态地将图像添加到Posts。图片属于Post,Post有很多图片。

发布模型:

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

图片模型:

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

我有一个表单,我想动态添加和删除图像。如果图像是正在上传的新图像,我想显示file_field;否则,如果图像已经存在,我想显示图像和删除链接。我跟着Ryan Bates在嵌套表格上的Railscasts。动态添加图像是有效的,但是对_destory的调用没有触发。帖子包含“_destroy”=&gt;“1”,但在HTTP Post事件之后,id为7的图像仍然存在。以下是HTTP Post的摘录:

"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事件中删除。

先谢谢你;我感谢任何花时间帮助我找到解决方案的人。

编辑:

- 解决方案 -

在Post Controller中,在更新方法的find中添加了“.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

1 个答案:

答案 0 :(得分:9)

我有一个非常相似的问题。我能够通过删除模型中的:reject_if子句来“解决”它。

 accepts_nested_attributes_for :images, :allow_destroy => true

删除后,我可以毫无问题地删除字段。明显的缺点是用户现在可以添加空白/无效图像,模型不会拒绝它。

编辑:

看起来这实际上是Rails 3.0.4关于accepts_nested_attributes_for如何处理:allow_destroy和:reject_if的错误。请参阅此处的答案,了解另一种解决方法:Paperclip problem for accepts_nested_attributes_for and reject_if

相关问题