Rails, route_path breaks when there is @ symbol in username

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 04:08:27

问题


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

No route matches {:controller=>"users", :action=>"show", :username=>"abc@shin.com"}

My routes looks like this:

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

And my view:

<% 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.


回答1:


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 => /.*/ }

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



来源:https://stackoverflow.com/questions/7407947/rails-route-path-breaks-when-there-is-symbol-in-username

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