2012-12-14 20 views
1

我有我的形象資產的拇指版本標準大拇指:Carrierwave;覆蓋「拇指」的方法來對非圖像資產

version :thumb, :if => :image? do 
    process :resize_to_fill => [118, 100] 
end 

現在,我得到一個RoutingError我非圖像資產。我試圖黑客拇指方法在AssetUploader.rb:

def thumb 
    if has_image_extension?(file.original_filename) 
    super 
    else 
    "/assets/file_types/#{extension(file.original_filename)}.jpg" 
    end 
end 

這會產生一個錯誤(NoMethodError:超:沒有超類方法`拇指'),這是有意義的。

有沒有比在資產模型中生成thumb_url方法更好的解決方法?

回答

0

首先version是一個CarrierWave類實例的方法。第一個參數應該是一個將作爲字符串附加到保存的文件名的符號。

但是,:if符號收到一個方法名稱(需要實現)來決定是否創建版本。

問題是你的:image?方法是如何實現的!

我有一個天真的實現它,在這裏:

def image?(new_file) 
    # TODO FIXME wrong way to check for image 
    new_file.content_type.include?('image') && 
    !new_file.content_type.include?('photoshop') && 
    !new_file.content_type.include?('svg') 
end