2012-08-27 89 views
0

這是當我嘗試上傳不是圖像的東西時獲得的消息,例如mp3。Ruby on rails - 無法使用MiniMagick/CarrierWave Uploader進行操作

Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid

所以我試圖通過檢查文件擴展名來提供一個條件。只有調整大小,如果它不是一個MP3。

下面是使用CarrierWave我FileUploader:

class FileUploader < CarrierWave::Uploader::Base 

include CarrierWave::MiniMagick 

... 

if File.extname(File.name) != ".mp3" 
    process :resize_to_fit => [100, 100] 

    version :thumb do 
    process :resize_to_fit => [80, 80] 
    end 

end 


... 

end 

File.name提供我只有名稱,沒有當前文件的擴展名。你知道給我提供名字+擴展名的變量嗎?

編輯:

我已經找到一個替代在我的控制器:

def create 
    @myfile = File.new(params[:icon]) 

    if @myfile.save 

     if @myfile.file.file.extension != "mp3" 
      @myfile.file.resize_to_fit(100, 100) 

      @file.save 
     end 
    end 

但現在我堅持了我的CarrierWave FileUploader:

version :thumb do 
    process :resize_to_fit => [80, 80] 
    end 

它變得太複雜,我只需要MiniMagick圖像

我只需要一個小條件:

if file_is_image? ==>調整大小+生成縮略圖

別的==>無能爲力

感謝

+0

剛一說明:爲什麼你有兩個minimagick和rmagick包括在內? – Tigraine

+0

我實際上只使用MiniMagick這只是一個錯誤,謝謝。 – user1560922

回答

1
process :resize_to_fit => [100, 100]; :if => :processable? 

def processable? upload_name 
    File.extname(upload_name.path) != ".mp3" 
end 
+0

未定義的局部變量或方法'文件名'。 File.name僅爲我提供沒有當前文件擴展名的名稱。你知道給我提供名字+擴展名的變量嗎?謝謝 – user1560922

+0

嘗試通過'model.file.name'來訪問它 – apneadiving

+0

同樣的錯誤:未定義的局部變量或方法'model' – user1560922