如何在 url Rails 中编码 (.) 点 [英] How to encode (.) Dot in url Rails

查看:44
本文介绍了如何在 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 <3).您应该升级您的应用程序.

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

这篇关于如何在 url Rails 中编码 (.) 点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆