简体   繁体   English

Rails url_for Gravatar路由错误

[英]Rails url_for Gravatar routing error

This function is defined in the application_help.rb: 此函数在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

It's used in views: 它在视图中使用:

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

Occasionally, its usage will result in a routing error: 有时,其用法将导致路由错误:

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

A valid email is being supplied when the error occurs. 发生错误时,将提供有效的电子邮件。

If I replace the url_for() with this logic, it works as expected: 如果我用以下逻辑替换url_for(),它将按预期工作:

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

** edit ** I had removed the following line from the routes.rb file: **编辑**我从route.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)))'

Is there a way to get the url_for to work without the 'legacy wild controller route'? 有没有一种方法可以使url_for在没有“旧式狂野控制器路线”的情况下工作?

You might want to take a look at the Gravtastic plugin for Rails which supports Gravatar images in both Ruby and JavaScript. 您可能想看看Rails的Gravtastic插件,该插件在Ruby和JavaScript中都支持​​Gravatar图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM