2011-03-31 41 views
2

此功能在application_help.rb定義:Rails的url_for的Gravatar路由錯誤

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在沒有'傳統野外控制器路線'的情況下工作?

回答

0

您可能想看看支持Rails的Gravtastic插件,它支持Ruby和JavaScript中的Gravatar圖像。