2013-01-22 29 views
0

使用Carrierwave上傳圖片, 我想寫1個函數來處理我的所有圖片包含幾種尺寸。如何將變量傳遞給像@ photo_main.file.url(:img_120x120)這樣的方法調用?

image_tag @photo_main.file.url(:img_122x145) rescue nil 

的:img_120x120在Carrierwave上傳定義,但爲什麼:它的名字前img_120x120分號?這是什麼格式?

通緝結果:

def get_avatar(size) 

    image_tag @photo_main.file.url(size) rescue nil 

end 

怎麼能這樣做?

UPDATE 1:

與失敗:::的ActionView模板::錯誤(未定義的方法`文件」的零:NilClass): 1:.ruler 2: 3:// = show_avatar_profile (@ profile.id) 4:= show_avatar_new(@ profile.id, 「96×96」)

def show_avatar_new(id, size) 

    puts "size is" 
    size = size.to_sym 
    puts size 

    @photo_main = Photo.where(:attachable_id => id, :attachable_type => "Profile", :main => true, :moderated => true, :approved => true).first 
    @photo = Photo.where(:attachable_id => id, :attachable_type => "Profile", :moderated => true, :approved => true).first 

    if @photo_main 
     image_tag @photo_main.file.url(size) 
    else 
     image_tag @photo.file.url(size) 
    end 

    end 

更新2:

class PhotoUploader < CarrierWave::Uploader::Base 

    include CarrierWave::RMagick 
    CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/ 

    storage :file 

    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 


    version :img_48x48 do 
    process :resize_to_fill => [48, 48] 
    end 

    version :img_58x58 do 
    process :resize_to_fill => [58, 58] 
    end 

    version :img_75x75 do 
    process :resize_to_fill => [75, 75] 
    end 

    version :img_96x96 do 
    process :resize_to_fill => [96, 96] 
    end 

    # Used in search results, 
    version :img_122x145 do 
    process :resize_to_fill => [122, 145] 
    end 

    version :img_200x200 do 
    process :resize_to_fill => [200, 200] 
    end 


    protected 

    def secure_token(length=32) 
    var = :"@#{mounted_as}_secure_token" 
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2)) 
    end 

    def delete_empty_upstream_dirs 
    path = ::File.expand_path(store_dir, root) 
    Dir.delete(path) # fails if path not empty dir 

    path = ::File.expand_path(base_store_dir, root) 
    Dir.delete(path) # fails if path not empty dir 
    rescue SystemCallError 
    true # nothing, the dir is not empty 
    end 

end 
+0

我不確定'url(size)'是在CarrierWave中獲取特定版本圖像的正確方法,您是否也可以發佈您的上傳器代碼? –

回答

2

在Ruby中,以冒號開頭的東西:(不是分號;!)是符號,它們本質上是不可變的字符串。

"img_122x145".to_sym # => :img_122x145 

看起來好像你寫的正是你所需要的。如果你想知道在哪裏把它,你可以把它放在一個輔助

# app/helpers/avatar_helper.rb 
def get_avatar(size) 
    image_tag @photo_main.file.url(size) 
end 

,請不要使用rescue nil那裏,雖然。你想抓住什麼錯誤?明確避免它而不是將異常用作流量控制會好得多。

image_tag @photo_main.file.url(size) if @photo_main.file? 

將足以避免@photo_main的問題,而無需一個file,並且更加意向揭示(並且,實際上,更高性能的)。最壞的情況下,你還是應該明確說明你希望得到

def get_avatar(size) 
    image_tag @photo_main.file.url(size) 
rescue SomeSpecificErrorThatCantBeAvoided 
    nil 
end 

This short (<3min) screencast使得避免直列救援的優秀案例是什麼類型的錯誤。如果你想獲得

@photo.file.img_122x145.url 

: -


更新

當您在CarrierWave版本,它創建的方法來訪問他們,你不要一個參數傳遞給url可變版本,但它們可通過versions(散列)獲得:

size = :img_122x145 
@photo.file.versions[size].url 

這不會解決您剩餘的問題,只是您的查詢找不到任何東西。

+0

謝謝你的偉大的寫作和澄清。事實上,救援行動非常糟糕,它是我一年前寫的一個老方法,當時我就開始使用導軌。我試圖讓載波顯示默認圖像,也失敗了。我重構並回復感謝很多 – Rubytastic

+0

通過將變量轉換爲.to_sym嘗試了您的建議,但似乎並沒有解決,它仍然失敗,你有什麼想法我錯過了什麼? – Rubytastic

+0

如果您將參數作爲像'get_avatar(:img_122x145)'這樣的符號傳遞,則不需要'to_sym'。錯誤信息告訴你你試圖在'nil'上調用'file',這意味着你的'@ photo_main'和'@ photo'必須都是'nil'。 –