paperclip + gravatar
-
05-07-2019 - |
题
我找到了这个教程( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/ )关于将gravatar作为默认图像实现到启用了纸夹的模型,但在实现时,请参见[:format,:png]的消息“未定义方法`匹配”:数组“。这篇文章有什么不对吗?
解决方案
我已经更新了代码,以便您更容易理解和调试。
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
其他提示
注意尝试此解决方案时出现以下错误:
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]
Paperclip.interpolates :gravatar_url do |attachment, style|
attachment.instance.gravatar_url(attachment.styles[style][:geometry].split('x').first)
end
如果您仍然遇到问题,可以尝试 Avatar gem,它支持链条不同的头像方法,包括Paperclip和Gravatar。
注意:这是一个无耻的插件,因为我写了这个东西。
不隶属于 StackOverflow