2015-04-06 93 views
2

两个模型共享相同的Carrierwave上传类。Carrierwave的多个默认图像

Carrierwave作为default_url方法。我怎样才能让它根据调用它的类返回不同的结果? e.g:

class PhotoUploader < CarrierWave::Uploader::Base 


def default_url 
    return "https://www.example.com" + ActionController::Base.helpers.asset_url("main/profile/user_2.png") if CONDITION   
    "https://www.example.com" + ActionController::Base.helpers.asset_url("main/profile/user.png") 
end 
+1

为什么不使用2个不同的上传者?我想你首先只使用了一个Uploader,因为两个模型共享相似之处。在你的情况下,现在,他们没有相似之处,他们是“不同的”,那么为什么要为两个需要特定处理的不同模型执行相同的代码呢? – MrYoshiji

回答

3

你可以调用模型和所有你需要例如方法:model.class.name

def default_url 
    return "https://www.example.com" + ActionController::Base.helpers.asset_url("main/profile/user_2.png") if model.class.name == "User"   
    "https://www.example.com" + ActionController::Base.helpers.asset_url("main/profile/user.png") 
end