开发者

Rails, route_path breaks when there is @ symbol in username

开发者 https://www.devze.com 2023-04-04 20:34 出处:网络
I get a RoutingError whenever a username has an @ symbol in it: No route matches {:controller=>\"users\", :action=>\"show\", :username=>\"abc@shin.com\"}

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号