質問

このチュートリアルを見つけました( http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-avatar-with-paperclip/ ) gravatarをpaperclip対応モデルのデフォルトイメージとして実装することについてですが、実装時に[:format、:png]:Array"の" undefined method `match 'というメッセージが表示されます。この記事の何が間違っていますか?

役に立ちましたか?

解決

コードを更新して、理解とデバッグが容易になるようにしました。

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

with

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

引き続き問題が発生する場合は、アバターを試してみてください。 PaperclipとGravatarの両方を含む、さまざまなアバターメソッド。

NB:これを書いたので、これは少し恥知らずなプラグインです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top