2

この関数は application_help.rb で定義されています。

  def gravatar_url_for(email, options = {})

    url_for(
      {
        :protocol => 'http://',
        :host => 'www.gravatar.com',
        :controller => 'avatar',
          # :controller => 'avatar.php',
        :gravatar_id => Digest::MD5.hexdigest(email),
        :only_path => false
      }.merge(options)
    )

  end

ビューで使用されます:

<%= image_tag(gravatar_url_for user.email, {:d => 'identicon', :s => 32, :r => 'g'}) %>

場合によっては、その使用によりルーティング エラーが発生します。

No route matches {:controller=>"avatar", :d=>"identicon", :r=>"g", :gravatar_id=>"486575e581db04b7c8ca218af8488657", :s=>32}

エラーが発生したときに有効な電子メールが提供されています。

url_for() をこのロジックに置き換えると、期待どおりに動作します。

url_for("http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(email) + "?d=identicon&s=40&r=g")

** 編集 ** routes.rb ファイルから次の行を削除しました。

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'

「レガシーワイルドコントローラールート」なしで url_for を機能させる方法はありますか?

4

1 に答える 1

0

Ruby と JavaScript の両方で Gravatar 画像をサポートする、Rails 用のGravtasticプラグインを確認することをお勧めします。

于 2011-10-22T20:51:53.130 に答える