【问题标题】:routing error with :username in url路由错误:url 中的用户名
【发布时间】:2011-07-09 09:51:39
【问题描述】:

我有一个 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) %>

引发异常:

没有路由匹配 {:controller=>"users", :action=>"show", :username=>".G"}

对不起,我的英语不好,谢谢!

【问题讨论】:

  • 您在此处的 link_to 路径使用什么?你如何格式化“用户名”参数?

标签: ruby-on-rails-3 routing


【解决方案1】:

我认为 Rails 路由默认不支持以 . 开头。你可以做类似的事情

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

上述正则表达式将匹配., a-z, A-Z, 0-9 and _ 作为用户名的有效字符。

【讨论】:

  • /[\.\%a-zA-Z0-9\-_]+/ 是我用来允许所有可能字符的正则表达式。它对我有用。
猜你喜欢
  • 2015-04-15
  • 2012-02-16
  • 2012-06-18
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多