Rails / Paperclip - skipping image processing

我的梦境 提交于 2019-12-24 01:54:27

问题


How can I skip post processing based on a virtual attribute?

My virtual attribute is always nil in the before_asset_post_process callback

Create

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

Attachable model

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

回答1:


Do you use attr_accessible in your Attachment model? If so, and if it does not include skip_thumb this fails (silently) when you try to assign it via mass assignment.

The opposite of attr_accessible is attr_protected, if you have skip_thumb in there, remove it.




回答2:


The assignment of :asset will happen before the assignment of :skip_thumb if it's first in the hash that you pass to Attachment.create(). Therefore, it will work if you change your code to:

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

I hope this is not too late to be useful...



来源:https://stackoverflow.com/questions/13203171/rails-paperclip-skipping-image-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!