clip + gravatar
-
05-07-2019 - |
Pregunta
Encontré este tutorial ( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/ ) sobre la implementación de gravatar como imagen predeterminada en el modelo habilitado para el clip, pero en la implementación veo el mensaje " método indefinido `match 'para [: format,: png]: Array " ;. ¿Qué está mal en este artículo?
Solución
He actualizado el código para facilitar la comprensión y la depuración.
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
Otros consejos
Note que recibí el siguiente error al intentar esta solución:
NoMethodError: undefined method `first' for #<Hash:0xb6476178>
from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url'
Lo resolví reemplazando la línea:
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
Si continúa teniendo problemas, puede probar la gema Avatar , que admite una cadena de diferentes métodos de Avatar, incluidos Paperclip y Gravatar.
NB: esto es un poco descarado, ya que lo escribí.