简体   繁体   English

URL和Rails路由系统中的双斜杠

[英]Double slashes in URL and rails route system

In my Rails application two URL such as 在我的Rails应用程序中,两个URL如

http://myurl/foo/bar
http://myurl/foo//bar

Are processed the same way. 以相同的方式处理。 It's pretty smart from Rails to ignore the empty attributes contained between slashes but it becomes a problem when I link everything with the front-end Javascript framework ; 从Rails忽略斜线之间包含的空属性是很聪明的,但是当我将所有内容与前端Javascript框架链接时,这将成为一个问题; my front-end routing system blows up because of it. 我的前端路由系统因此而崩溃。

In my opinion, this should be understood as a problem in the URL by Rails because it seems more strict and logical. 我认为,这应该被Rails理解为URL中的一个问题,因为它看起来更加严格和合乎逻辑。

Therefore, I would like to simply redirect the user to the homepage when the routes.rb detects a // within the path ; 因此,当routes.rb在路径中检测到//时,我想简单地将用户重定向到首页; how is this possible ? 这怎么可能 ?

Thank you ;) 谢谢 ;)

EDIT : 编辑:

I tried this kind of things 我尝试过这种事情

match ':url' => redirect('/'), :constraints => { url: /\/+/ }, via: [:get, :post]

And it doesn't work, seems like the route file is read after rails manipulate the absolute URL first, or something like that ... 而且它不起作用,似乎是在Rails首先操纵绝对URL或类似的东西之后读取了路由文件...

I would try writing a regular expression the validates the route before it is passed to the specific controller to process the route. 我会尝试编写一个正则表达式来验证路由,然后再将其传递到特定控制器以处理该路由。 If the validation proves that the url is invalid it redirects to the home route 如果验证证明该网址无效,它将重定向到本地路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM