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

天涯浪子 提交于 2019-12-24 03:22:17

问题


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

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