routing error with :username in url

喜你入骨 提交于 2019-11-30 18:14:24

问题


I have a Rails 3 question. I want to get my users controller show page to have the app.com/people/username url.

Route

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

It's work, but if username starts with "." (dot) I have an error:

No route matches "/people/.G"

And

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

raise an exception:

No route matches {:controller=>"users", :action=>"show", :username=>".G"}

Sorry for my English, Thanks!


回答1:


I don't think starting with . is supported by default in rails routes. You can do something like

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

The above regex will match ., a-z, A-Z, 0-9 and _ as valid characters for the username.



来源:https://stackoverflow.com/questions/5204057/routing-error-with-username-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!