本文介绍了网址中带有:username的路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Rails 3问题。我想让我的用户控制器显示页面具有app.com/people/username url。
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:
没有路线匹配 /people/.G
和
<%= link_to current_user.username, profile_path(current_user.username) %>
引发例外:
没有路由匹配{:controller => users,:action => show,:username =>。G}
对不起
推荐答案
我不认为以开头。$ c $ Rails路由默认支持c>。您可以执行以下操作
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_]+/
上述正则表达式将匹配。,az,AZ,0-9和_
作为用户名的有效字符。
The above regex will match ., a-z, A-Z, 0-9 and _
as valid characters for the username.
这篇关于网址中带有:username的路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!