1

我发现本教程(http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/)关于将 gravatar 实现为默认图像到启用回形针的模型,但在实现时我看到消息“未定义的方法 `match' for [:format, :png]:Array”。这篇文章有什么问题?

4

4 回答 4

2

我更新了代码,让您更容易理解和调试。

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
于 2009-10-23T03:10:34.113 回答
2

请注意,尝试此解决方案时出现以下错误:

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

我通过替换线解决了它:

size_data = attachment.styles[style].first

size_data = attachment.styles[style][:geometry]
于 2009-12-31T16:46:36.423 回答
1
Paperclip.interpolates :gravatar_url do |attachment, style|
    attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
于 2013-12-28T18:17:25.927 回答
0

如果您仍然遇到问题,您可以尝试Avatar gem,它支持一系列不同的 Avatar 方法,包括 Paperclip 和 Gravatar。

注意:这是一个有点无耻的插件,因为我写了这个东西。

于 2009-10-23T16:49:37.617 回答