graffetta + gravatar
-
05-07-2019 - |
Domanda
Ho trovato questo tutorial ( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/ ) sull'implementazione di gravatar come immagine predefinita sul modello abilitato per graffette, ma sull'implementazione vedo il messaggio "metodo indefinito" match "per [: format,: png]: Array " ;. Cosa c'è di sbagliato in questo articolo?
Soluzione
Ho aggiornato il codice per semplificare la comprensione e il 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
Altri suggerimenti
Nota Quando ho tentato questa soluzione ho riscontrato il seguente errore:
NoMethodError: undefined method `first' for #<Hash:0xb6476178>
from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url'
L'ho risolto sostituendo la riga:
size_data = attachment.styles[style].first
con
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
Se continui ad avere problemi, puoi provare la Avatar , che supporta una catena di diversi metodi Avatar, inclusi Paperclip e Gravatar.
NB: questa è una sorta di spina spudorata, dal momento che ho scritto la cosa.