本文介绍了用户名中有@符号时,Rails,route_path中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户名中包含 @ 符号时,我都会遇到路由错误:

I get a RoutingError whenever a username has an @ symbol in it:

No route matches {:controller=>"users", :action=>"show", :username=>"[email protected]"}

我的路线如下:

match 'users/:username' => 'users#show', :as => :show_other_user

我的观点:

<% for user in @users %>
  <tr>
    <td><%= image_tag avatar_url(user, 50) %></td>
    <td><%= link_to user.name, show_other_user_path(:username => user.username) %></td>
    <td><%= common_friends(current_user, user).size %></td>
  </tr>
<% end %>

一切都可以找到用户名是否没有 @ 符号。

Everything works find if the username doesn't have the @ symbol.

推荐答案

由于 @ ,由于而中断。 Rails将看到,并认为您正在尝试指定一种格式(即 .html .xml ,...)。您需要通过使用以下方式更新路由来稍微了解一下自动格式化检测的内容:

Your route isn't breaking because of the @, it is breaking because of the .. Rails will see the . and think you're trying to specify a format (i.e. .html, .xml, ...). You need to kludge around the auto-format detection stuff a little bit by updating your route with something like this:

match 'users/:username' => 'users#show', :as => :show_other_user, :constraints => { :username => /.*/ }

:约束应该解决您的路由问题(对于Rails 2,您可以使用:requirements )。

The :constraints should sort out your routing problems (you'd use :requirements for Rails 2).

这篇关于用户名中有@符号时,Rails,route_path中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:37