问题
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