Pergunta

Eu encontrei este tutorial ( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/ ) sobre a implementação de gravatar imagem padrão como o modelo habilitado para clipe de papel, mas na implementação i ver mensagem "undefined method` match' para [: formato,: png]: array". O que está errado neste artigo?

Foi útil?

Solução

Eu tenho atualizado o código para tornar mais fácil para você entender e depurar.

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

Outras dicas

Note que eu tenho o seguinte erro ao tentar esta solução:

NoMethodError: undefined method `first' for #<Hash:0xb6476178>
    from /home/bob/dev/Firehoze/app/models/user.rb:114:in `gravatar_url'

Eu resolvi-lo substituindo a linha:

size_data = attachment.styles[style].first

com

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 você continuar a ter problemas, você poderia tentar o Avatar gem, que suporta uma cadeia de métodos diferentes Avatar, incluindo tanto Paperclip e Gravatar.

NB:. Isso é um pouco de um plug descarado, desde que eu escrevi a coisa

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top