[英]custom attributes version of accepts_nested_attributes_for with reject_if
[英]Paperclip problem for accepts_nested_attributes_for and reject_if
我正在開發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
問題是“ _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仍然是必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.