简体   繁体   English

accepts_nested_attributes_for和reject_if的回形针问题

[英]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.

相关问题 带reject_if的accepts_nested_attributes_for的定制属性版本 - custom attributes version of accepts_nested_attributes_for with reject_if accepts_nested_attributes_for:reject_if触发另一个方法 - accepts_nested_attributes_for :reject_if to trigger another method Rails如何在accepts_nested_attributes_for时拒绝_if - Rails how to reject_if when accepts_nested_attributes_for rails accepts_nested_attributes_for:reject_if无效 - rails accepts_nested_attributes_for :reject_if not working Rails 3:“accepts_nested_attributes_for:reject_if”不起作用 - Rails 3: “accepts_nested_attributes_for :reject_if” doesn't work 有没有一种方法可以仅在创建时就为acpe强制执行accept_nested_attributes_for? - Is there a way to enforce reject_if for accep accepts_nested_attributes_for only on create? 如何在更新期间使用accepts_nested_attributes_for的reject_if选项删除子级 - How to delete child during update with reject_if option for accepts_nested_attributes_for rails accepts_nested_attributes和:reject_if - rails accepts_nested_attributes and :reject_if accepts_nested_attributes_for,reject_if和has_one关系不能一起工作? - accepts_nested_attributes_for, reject_if and has_one relationship not working together? 工厂女孩:创建忽略accepts_nested_attributes_for上的reject_if - Factory-Girl :create ignores reject_if on accepts_nested_attributes_for
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM