2013-02-15 40 views
4

我需要在某些验证功能后安装图片上传器。条件下的导轨载波安装

但是,如果我在模型调用像往常一样安装者:

mount_uploader :content, ContentUploader 

carrierwave先下载内容,然后Rails的启动模式的验证。

具体来说,我根本不想加载大文件!我想检查http标头Content-lengthContent-type,然后,如果没问题,请挂载上传器。

也许这样的事情:

if condition 
    mount_uploader :content, ContentUploader 
end 

我该怎么办呢?

P.S. Rails版本3.2.12

回答

1

如果你只是想避免加载大文件,这是不是要走的路!也就是说,可以有条件挂载覆盖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