accepts_nested_attributes_for和reject_if的回形针问题

我正在开发rails 3应用程序。

class Post  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  { :thumb => "100x100#", :small => "150x150>", :mid => "640x640>", :large => "800x800>" } end 

问题是“_destroy”=>“1”不适用于附件和照片。 我想出如果我删除reject_if选项,它就可以了。 怎么了?

谢谢。

山姆

好像自Rails 3.0.3以来,需要加载要销毁的关联(附件,照片)。 看看这张票 。 快速修复,不是那么优雅,是在您的更新方法中加载您的关联:

 @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仍然需要这样做。