我需要在一些验证功能后挂载图片 uploader 。
但是如果我像往常一样在模型中调用安装 uploader :
mount_uploader :content, ContentUploader
carrierwave先下载内容,然后Rails开始验证模型。
具体来说,我根本不想加载大文件!我想检查 http header Content-length
和 Content-type
然后,如果没问题,挂载 uploader。
也许是这样的:
if condition
mount_uploader :content, ContentUploader
end
我该怎么做?
附言Rails 版本 3.2.12
最佳答案
如果你只是想避免加载大文件,这不是要走的路!也就是说,可以有条件挂载覆盖 content=
。
CarrierWave v1.1.0 仍然没有条件挂载。但注意 mount_uploader
首先 includes a module在类里面然后 overrides原来content=
调用方法content=
defined在包含的模块中。因此,解决方法是在调用 mount_uploader
之后 重新定义访问器:
class YourModel < ActiveRecord::Base
mount_uploader :content, ContentUploader
def content=(arg)
if condition
super
else
# original behavior
write_attribute(:content, arg)
end
end
def content
# some logic here
end
end
关于ruby-on-rails - Rails carrierwave 安装条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895743/