2011-01-22 16 views
2

私はレール3アプリケーションを開発中です。accepts_nested_attributes_forとreject_ifのペーパークリップの問題

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以来、あなたは(添付ファイル、写真)を破壊したい団体をロードする必要があるよう

サム

答えて

1

は思えます。 this ticketをご覧ください。あまりエレガントではない簡単な修正は、更新メソッドにあなたの関連付けをロードすることです:

@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これはRails 3.0.4ではまだ必要です。

+1

うん。チケットは私によって発行されました:-)どうもありがとう。 –

+0

おっと私はそれを見ます。まあまもなく本物の修正を希望します。 – mud

+0

アソシエーションをロードしても私の問題は解決しませんでした。 – Eytan

関連する問題