2009-10-22 71 views

回答

2

我更新了代碼,使其更容易理解和調試。

Paperclip.interpolates(:gravatar_url) do |attachment, style| 
    size = nil 
    # style should be :tiny, :small, or :regular 
    # size_data is assumed to be "16x16#", "20x20#", or "25x25#", i.e., a string 
    size_data = attachment.styles[style][:geometry] 
    if size_data 
    # get the width of the attachment in pixels 
    if thumb_size = size_data.match(/\d+/).to_a.first 
     size = thumb_size.to_i 
    end 
    end 
    # obtain the url from the model 
    # replace nil with "identicon", "monsterid", or "wavatar" as desired 
    # personally I would reorder the parameters so that size is first 
    # and default is second 
    attachment.instance.gravatar_url(nil, size) 
end 
+0

什麼風格像:thumb => ['75x75#',::PNG] ?? – 2009-10-23 04:16:01

+0

也 - 我已更新此代碼 Paperclip.interpolates:gravatar_url do | attachment,style | 和 attachment.instance.gravatar_url(「」,size) – 2009-10-23 04:17:37

+0

正確...這就是爲什麼我添加了所有這些評論。 '['75x75#',::png]'會導致錯誤,因爲文章假設'size_data'是一個字符串。如果不是,那麼你需要更新代碼來做:'size_data = size_data.first if size_data.kind_of?(Array)'或類似的東西。 – 2009-10-23 04:40:28

0

如果你仍然有問題,你可以試試Avatar寶石,它支持的不同化身的方法,包括回形針和的Gravatar鏈。

注意:這是一個無恥的插件,因爲我寫了這個東西。

+0

是啊,我見過,但沒有足夠的文檔來理解如何使用你的寶石:( – 2009-10-23 18:40:16

2

注意我嘗試此解決方案時,出現以下錯誤:

NoMethodError: undefined method `first' for #<Hash:0xb6476178> 
    from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url' 

我解決它通過更換行:

size_data = attachment.styles[style].first 

size_data = attachment.styles[style][:geometry] 
1
Paperclip.interpolates :gravatar_url do |attachment, style| 
    attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first) 
end