trombone + gravatar
-
05-07-2019 - |
Question
J'ai trouvé ce tutoriel ( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a- fallback-avatar-with-paperclip/ ) sur l'implémentation de gravatar en tant qu'image par défaut dans le modèle utilisant un trombone, mais pour l'implémentation, je vois le message "méthode non définie" match "pour [: format,: png]: Array". Quel est le problème dans cet article?
La solution
J'ai mis à jour le code pour faciliter la compréhension et le débogage.
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
Autres conseils
Notez que j'ai eu l'erreur suivante lors de la tentative de cette solution:
NoMethodError: undefined method `first' for #<Hash:0xb6476178>
from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url'
Je l'ai résolu en remplaçant la ligne:
size_data = attachment.styles[style].first
avec
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
Si le problème persiste, vous pouvez essayer le Avatar , qui prend en charge une chaîne de différentes méthodes Avatar, y compris Paperclip et Gravatar.
NB: c’est un peu un bouchon éhonté, puisque j’ai écrit la chose.