2012-11-02 81 views
2

如何跳过基于虚拟属性的后处理?Rails /回形针 - 跳过图像处理

我的虚拟属性总是无在before_asset_post_process回调

创建

可拆卸的模型

class Attachment < AR::Base 
attr_accessor :skip_thumb 

    has_attached_file :asset, :styles => lambda { |attachment| { :thumb => ["100>", 'jpg'] , 
                     :thumb_big => ["200>", 'jpg'] 
                    } 
    before_asset_post_process :proceed_or_cancel 

    def proceed_or_cancel 
    #self.skip_thumb is always nil 
    if (self.skip_thumb.present?) 
     return false 
    end 
    end 

end 
+0

不使用attr_accessible –

+0

这似乎是因为属性不被置直到before_asset_post_process之后 –

回答

0

你在你的Attachment模型中使用attr_accessible? 如果是这样,并且它不包括skip_thumb当您尝试通过质量分配分配时,失败(静默)。

attr_accessible的反义词是attr_protected,如果您有skip_thumb,请将其删除。

0

:asset的分配将在分配:skip_thumb之前发生,如果它首先位于散列中,那么您将传递给Attachment.create()。因此,如果你改变你的代码,它的工作:

attachment = Attachment.create(skip_thumb: 1, asset: File.open(file.png)) 

我希望这不是太晚有用...