如何在 url Rails 中编码 (.) 点

问题描述:

我有如下路线来删除/列出用户.

I am having routes like below to delete/list an user.

map.connect 'developer/:user_name/delete',:controller=>"developers",:action=>"delete",:method=>:delete  

map.connect 'developer/:user_name/list',:controller=>"developers",:action=>"list",:method=>:get

通过使用 %2E 编码点来列出用户时,我可以看到成功响应

While listing the user by encoding the Dot with %2E, i can see the success response

http://localhost:3000/developer/testuser%2Ehu/list

但是在尝试删除包含 Dot(.) 的用户时,抛出 404 错误.

But While trying to delete the user who containing the Dot(.), throws 404 error.

http://localhost:3000/developer/testuser%2Ehu/delete, how to fix this issue.

默认情况下,Rails 路由中不允许使用点,因为点被认为是页面的扩展.您应该避免在网址中使用点.

The dot is not allowed by default in Rails routing because the dot is considered the extension of the page. You should avoid using dots in URLs.

但是,在您的情况下,您可以指示 Rails 考虑传递正则表达式的 :user_name 参数的点.

However, in your case you can instruct Rails to consider the dot for the :user_name parameter passing a regular expression.

map.connect 'developer/:user_name/list', :controller => "developers", :action => "list", :method=> :get, :user_name => /[\w.]+/

附注.由于 map.connect,您使用的是非常旧的 Rails 版本(Rails

PS. Because of map.connect, you are using a very old version of Rails (Rails < 3). You should upgrade your application.