【发布时间】:2017-12-24 16:22:04
【问题描述】:
我一直在努力寻找任何教程或问题来解释如何根据用户提供的某些条件上传图像并调整其大小。
我可以使用硬编码值轻松上传和调整图像大小,但我一直坚持使用用户提供的参数从上传器访问。
我希望根据用户检查图像是大还是小,将图像大小调整为 800x600 或 300x300。
为此,我在模型结构中有一个名为“大”的布尔列。
在 Uploader 中,我可以在 store_dir 块中轻松访问模型及其值,但在此块之外的任何地方,任何模型属性都返回为 nil。
这就是我想做的:-
class BannerUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
resize_to_fit(800,600) if model.large==true
resize_to_fit(300,300) if model.large!=true
end
但是这会返回错误 BannerUploader:Class 的未定义局部变量或方法“模型”
如何解决这个问题。
【问题讨论】:
-
我觉得对你有帮助andreapavoni.com/blog/2012/3/…
标签: ruby-on-rails imagemagick carrierwave