问题
I would like to crop an image to the size the user has selected from a list (e.g. 100x100px, 200x200px,...) How would I pass that attribute to the uploader or get the model's attribute from within the uploader?
Accessing the model's attribute from within the uploader as following does not work:
version :thumb do
thumbnail_size = model.thumbnail_size
...
...
end
I get following error:
undefined local variable or method `model' for #
Thank you! Florian
回答1:
In order to be able to access the model's attribute I had to add a manipulation helper.
class MyUploader < CarrierWave::Uploader::Base
...
version :thumb do
process :custom_thumbnail
process :convert => 'jpg'
...
end
def custom_thumbnail
width = model.get_image_width
height = model.get_image_height
manipulate! do |img|
img.convert "#{width}x#{height}"
img
end
end
end
来源:https://stackoverflow.com/questions/7713628/passing-a-parameter-to-the-uploader-accessing-a-models-attribute-from-within