将参数传递给上传器/从上传器内部访问模型的属性/让用户选择缩略图大小 [英] Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size

查看:54
本文介绍了将参数传递给上传器/从上传器内部访问模型的属性/让用户选择缩略图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像裁剪为用户从列表中选择的大小(例如100x100px,200x200px等)
如何将属性传递给上载器或从内部获取模型的属性

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

我得到以下错误:


未定义的局部变量或方法`型号,用于#

undefined local variable or method `model' for #

谢谢!
Florian

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

这篇关于将参数传递给上传器/从上传器内部访问模型的属性/让用户选择缩略图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆