2011-01-22 75 views
2

我正在開發一個rails 3應用程序。回形針的問題accept_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選項,它的作品。 有什麼問題?

謝謝。

山姆

回答

1

好像以來的Rails 3.0.3,要摧毀(附件,附圖)聯想需要加載。看看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

是的。票是由我發出的。:-)無論如何。 – 2011-02-18 07:01:44