views:

254

answers:

3

Hi all. I've found this tutorial (http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-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?

+1  A: 

I've updated the code to make it easier for you to understand and debug.

Paperclip.interpolations(: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].first
  if size_data
    # get the width of the icon 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
Bob Aman
what about styles like :thumb => ['75x75#', :png] ??
Alexey Poimtsev
also - i've updated this code with Paperclip.interpolates :gravatar_url do |attachment, style|and attachment.instance.gravatar_url("", size)
Alexey Poimtsev
Right... that's why I added all those comments. `['75x75#', :png]` will cause an error because the article assumes `size_data` is a string. If it's not, then you need to update the code to do: `size_data = size_data.first if size_data.kind_of?(Array)` Or something similar.
Bob Aman
i've got undefined method `match' for :format:Symbol :((
Alexey Poimtsev
solved by "if thumb_size = size_data.to_s.match(/\d+/).to_a.first"
Alexey Poimtsev
No offense, but that's... a really bad solution.
Bob Aman
A: 

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.

James A. Rosen
yeah, i've seen. But there are not enought docs to understand how to use your gem :(
Alexey Poimtsev
+2  A: 

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]