2016-04-15 46 views
0

我發現this link解釋瞭如果上傳不是視頻,如何不製作縮略圖。載波上傳 - 圖像或視頻

但是,如果保存的文件IS或IS不是圖像,我該如何檢查? (在我的情況下,我可以存儲圖像或視頻),通過調用持有上傳器的模型的方法?我是否需要在is_image?(new_file)中進行某種回調,以便在模型上設置一個字段?

我希望能夠調用@model.is_image?@model.iI would bes_video?或者甚至像case @model.type; when :video ; ...

回答

0

我添加了一個回調上保存上傳模型的屬性集

# uploader/my_uploader.rb 
... 
def image?(new_file) 
    if new_file.content_type.include? 'image' 
    model.image = true 
    true 
    else 
    false 
    end 
end 

# models/my_model.rb (mongoid) 
... 
field :image, type: Boolean 
mount_uploader MyUploader 

# View 
@my_model.image?