paperclip + gravatar
I've found this tutorial (http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-开发者_如何学Goavatar-with-paperclip/) about implementing gravatar as default image to the paperclip-enabled model, but on implementing i see message "undefined method `match' for [:format, :png]:Array". Whats wrong in this article?
I've updated the code to make it easier for you to understand and debug.
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
Note I got the following error when attempting this solution:
NoMethodError: undefined method `first' for #<Hash:0xb6476178>
from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url'
I solved it by replacing the line:
size_data = attachment.styles[style].first
with
size_data = attachment.styles[style][:geometry]
Paperclip.interpolates :gravatar_url do |attachment, style|
attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
If you continue to have trouble, you could try the Avatar gem, which supports a chain of different Avatar methods, including both Paperclip and Gravatar.
NB: this is a bit of a shameless plug, since I wrote the thing.
精彩评论