[英]Paperclip problem for accepts_nested_attributes_for and reject_if
I am developing a rails 3 application. 我正在开发Rails 3应用程序。
class Post < ActiveRecord::Base
has_many :attachments
has_many :photos
accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attrs| attrs['document'].blank? }
accepts_nested_attributes_for :photos, :allow_destroy => true, :reject_if => proc { |attrs| attrs['image'].blank? }
end
class Attachment < ActiveRecord::Base
belongs_to :post
has_attached_file :document
end
class Photo < ActiveRecord::Base
belongs_to :post
has_attached_file :image, :styles => {
:thumb => "100x100#",
:small => "150x150>",
:mid => "640x640>",
:large => "800x800>"
}
end
The problem is that "_destroy"=>"1" doesn't work for attachments and photos. 问题是“ _destroy” =>“ 1”不适用于附件和照片。 I figured out that if I remove reject_if option, it works.
我发现,如果我删除reject_if选项,它将起作用。 What's wrong?
怎么了?
Thanks. 谢谢。
Sam 山姆
Seems like since Rails 3.0.3, the association that you want to destroy (attachments, photos) need to be loaded. 好像从Rails 3.0.3起,您要破坏的关联(附件,照片)需要被加载。 Take a look at this ticket .
看一下这张票 。 A quick fix, which isn't so elegant is to load your association in your update method:
一个快速的解决方法(不是那么好用)是在更新方法中加载关联:
@post = Post.includes(:attachments).find(params[:id])
if @post.update_attributes(params[:post])
redirect_to(posts_url, :notice => 'Post updated.'
else
render :action => "edit"
end
FYI this is still necessary for Rails 3.0.4. 仅供参考,这对于Rails 3.0.4仍然是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.