Rails / Paperclip - 跳过图像处理

3

如何基于虚拟属性跳过后处理?

在 before_asset_post_process 回调中,我的虚拟属性始终为 nil。

创建

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

可附加模型

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

似乎是因为属性直到 before_asset_post_process 之后才被设置。 - recursive_acronym
我在Paperclip存储库中找到了有关此问题的详细信息,这些信息阐明了该问题https://github.com/thoughtbot/paperclip/issues/1279“处理的时间取决于何时进行附件的批量分配,因为处理发生在分配而不是保存时。” - ethaning
2个回答

0

如果在传递给Attachment.create()的哈希表中,:asset排在第一位,那么它的赋值将会在:skip_thumb之前进行。因此,如果你将代码更改为以下形式,它将能够正常工作:

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

希望这还有用...


0
你的Attachment模型中使用了 attr_accessible 吗? 如果使用了,并且它没有包括 skip_thumb,当你通过批量赋值尝试对其进行赋值时,这将失败(静默处理)。
attr_accessible 的相反是 attr_protected,如果其中有 skip_thumb,请将其删除。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接