Rails url_for Gravatar路由错误

时间:2011-03-31 16:17:53

标签: ruby-on-rails-3 routing gravatar url-for

此函数在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在没有'传统的野外控制器路由'的情况下工作?

1 个答案:

答案 0 :(得分:0)

你可能想看一下Rails的Gravtastic插件,它支持Ruby和JavaScript中的Gravatar图像。