Rails 3.0和Carrierwave如何防止非图像类型的版本创建
我正在努力尝试实现最初对我来说看似简单的事情。在使用 rierwave 的 rails 3.0 应用程序的上下文中。用户必须能够下载除 .exe 文件之外的任何类型的文档。在 Carriewave 中有一个白名单,
def extension_white_list
%w(jpg jpeg gif png)
end
我希望也有一个黑名单,这对我来说会更容易。无论如何,这不是主要问题。
对于图像文件,我在上传器类中设置了 2 个版本。
version :thumb do
process :resize_to_fit => [50, 50]
end
version :small do
process :resize_to_fit => [125, 125]
end
我必须承认我对这种语法有点困惑。什么样的 ruby 代码“版本”定义。我的意思是,它不是一个方法,那么它在类中是什么?
我尝试创建方法图像?
def image?
%w(jpg jpeg gif png).include?(filename.extension.to_s)
end
然后从版本代码中调用它,以防止在文件不是图像时创建这些版本
version :thumb do
if image?
process :resize_to_fit => [50, 50]
end
end
,但此代码会抛出错误,
undefined method `image?' for #<Class:0x000001017274f8>
任何帮助将不胜感激。
谢谢。
I'm struggling a bit trying to achieve something that appeared simple initially to me. In the context of a rails 3.0 app using carrierwave. The user must be able do download any type of document except .exe files. in Carriewave there is the whitelist
def extension_white_list
%w(jpg jpeg gif png)
end
I whish there was a blacklist as well it would easier in my case. Anyway this is the not the main concern.
for image file I set 2 version in my uploader class.
version :thumb do
process :resize_to_fit => [50, 50]
end
version :small do
process :resize_to_fit => [125, 125]
end
I have to admit that I'm little bit confused by this syntax. What kind of ruby code "version" define. I mean, it is not a method, so what's that in a class?
I tried to create a method image?
def image?
%w(jpg jpeg gif png).include?(filename.extension.to_s)
end
and then called it from the version piece of code to prevent creation of those version when the file is not an image
version :thumb do
if image?
process :resize_to_fit => [50, 50]
end
end
but this code throw an error
undefined method `image?' for #<Class:0x000001017274f8>
any help would be appreciated.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是您实际上可以如何做到这一点。当前版本的 Carrierwave 现在支持条件版本处理。请参阅 wiki 页面 https://github.com/jnicklas /rierwave/wiki/How-to%3A-Do-conditional-processing
要仅为特定类型创建版本,请执行以下操作:
Here's how you actually can do this. The current version of carrierwave supports conditional version processing now. See the wiki page https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing
To only create versions for specific types, do this:
最后对这个问题有一个清晰干净的答案:https://gist.github.com/995663
Finally a clear and clean answer for this question: https://gist.github.com/995663