开发者

Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size

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 loca开发者_如何学Cl variable or method `model' for #

Thank you! Florian


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜