首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回形针+头像

回形针+头像
EN

Stack Overflow用户
提问于 2009-10-23 06:23:21
回答 4查看 1K关注 0票数 1

我找到了这个教程(http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/),它是关于将gravatar实现为启用回形针的模型的默认图像,但在实现时,我看到了消息"undefined method ` `match‘for :format,:png:Array“。这篇文章有什么问题吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-10-23 11:10:34

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

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2010-01-01 00:46:36

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

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

我通过替换这行代码解决了这个问题:

代码语言:javascript
复制
size_data = attachment.styles[style].first

使用

代码语言:javascript
复制
size_data = attachment.styles[style][:geometry]
票数 2
EN

Stack Overflow用户

发布于 2013-12-29 02:17:26

代码语言:javascript
复制
Paperclip.interpolates :gravatar_url do |attachment, style|
    attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1610331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档