accepts_nested_attributes_for 和 reject_if 的回形针问题 [英] Paperclip problem for accepts_nested_attributes_for and reject_if
本文介绍了accepts_nested_attributes_for 和 reject_if 的回形针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 rails 3 应用程序.
I am developing a rails 3 application.
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
问题是_destroy"=>1"不适用于附件和照片.我发现如果我删除 reject_if 选项,它就可以工作.怎么了?
The problem is that "_destroy"=>"1" doesn't work for attachments and photos. I figured out that if I remove reject_if option, it works. What's wrong?
谢谢.
山姆
推荐答案
好像从 Rails 3.0.3 开始,你要销毁的关联(附件、照片)需要加载.看看这张票.一个不太优雅的快速修复方法是在更新方法中加载关联:
Seems like since Rails 3.0.3, the association that you want to destroy (attachments, photos) need to be loaded. 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
仅供参考,这对于 Rails 3.0.4 来说仍然是必要的.
FYI this is still necessary for Rails 3.0.4.
这篇关于accepts_nested_attributes_for 和 reject_if 的回形针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文