回形针 +头像

发布于 2024-08-08 20:27:16 字数 366 浏览 5 评论 0原文

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

I've found this tutorial (http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/) about implementing gravatar as default image to the paperclip-enabled model, but on implementing i see message "undefined method `match' for [:format, :png]:Array". Whats wrong in this article?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

维持三分热 2024-08-15 20:27:16

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

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

I've updated the code to make it easier for you to understand and 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
自演自醉 2024-08-15 20:27:16

请注意,在尝试此解决方案时,我收到以下错误:

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]

Note I got the following error when attempting this solution:

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

I solved it by replacing the line:

size_data = attachment.styles[style].first

with

size_data = attachment.styles[style][:geometry]
总攻大人 2024-08-15 20:27:16
Paperclip.interpolates :gravatar_url do |attachment, style|
    attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
Paperclip.interpolates :gravatar_url do |attachment, style|
    attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
国际总奸 2024-08-15 20:27:16

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

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

If you continue to have trouble, you could try the Avatar gem, which supports a chain of different Avatar methods, including both Paperclip and Gravatar.

NB: this is a bit of a shameless plug, since I wrote the thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文