为了简化我的附件图片代码,我创建了一个初始化器来覆盖Paperclip使用的@default_options变量。
这样,我就不必一遍又一遍地指定我想要的url、path和storage了。
我希望能更进一步,在其中包括验证,但我无法使其工作...
有什么想法吗?
编辑1:我想至少验证存在性和大小。
编辑2:我当前代码的一部分。
这篇博客:http://omgsean.com/2009/02/overriding-paperclip-defaults-for-your-entire-rails-app/展示了
因此,虽然尚未找到,但可能是可行的。
这样,我就不必一遍又一遍地指定我想要的url、path和storage了。
我希望能更进一步,在其中包括验证,但我无法使其工作...
有什么想法吗?
编辑1:我想至少验证存在性和大小。
编辑2:我当前代码的一部分。
module Paperclip
class Attachment
def self.default_options
if Rails.env != "production"
@default_options = {
:url => "/assets/:class/:attachment/:id/:style/:normalized_name",
:path => ":rails_root/public/assets/:class/:attachment/:id/:style/:normalized_name",
:default_style => :original,
:storage => :filesystem,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
}
else
...
end
end
end
normalized_name
是一个外部函数,特性:http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
编辑3:这篇博客:http://omgsean.com/2009/02/overriding-paperclip-defaults-for-your-entire-rails-app/展示了
default_options
哈希表中的validations
键。因此,虽然尚未找到,但可能是可行的。