2011-03-05 84 views
3

用戶名我有一個Rails 3的問題。我想讓我的用戶控制器顯示頁面具有app.com/people/username網址。與路由錯誤:在URL

路線

resources :users 
match "/people/:username" => 'users#show', :as => :profile 

它的工作,但如果用戶名開頭 「」 (點),我有一個錯誤:

沒有路由匹配 「/people/.G」

而且

<%= link_to current_user.username, profile_path(current_user.username) %> 

拋出異常:

沒有路由匹配{:控制器=> 「用戶」,:動作=> 「秀」,內容:username => 「G」}

對不起,我的英語,謝謝小號!

+0

什麼是您使用您的link_to的路徑嗎?你如何格式化「用戶名」參數? – goddamnyouryan 2011-08-27 09:24:18

回答

1

我不認爲有.開始默認情況下,在軌道線路的支持。你可以這樣做

match "/people/:username" => 'users#show', :as => :profile, :username => /[\.a-zA-Z0-9_]+/ 

上述正則表達式匹配., a-z, A-Z, 0-9 and _爲有效字符的用戶名。

+0

好的,現在它按預期工作,tyvm! – Deku 2011-03-05 13:37:10

+0

/[\.\%a-zA-Z0-9\-\_]+/是我用來允許所有可能的字符正則表達式。它爲我工作。 – Anand 2011-04-26 05:34:05