Rails, route_path breaks when there is @ symbol in username
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>
&开发者_如何学编程lt;% end %>
Everything works find if the username doesn't have the @
symbol.
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).
精彩评论