简体   繁体   English

Ruby on Rails-错误路线的正则表达式

[英]Ruby on Rails - Regular expression for error routes

How do I define a routes match any things exclude string ( like 'websocket' )? 如何定义与任何排除字符串(例如'websocket')的内容匹配的路由?

Thanks! 谢谢!

Based on the comments, it sounds like you want to match /websocket to a specific action and everything else to an 404 error page. 根据注释,听起来您想将/websocket与特定操作进行匹配,将其他所有内容与404错误页面进行匹配。

Utilizing the fact that routes are matched in the order they are defined in routes.rb , this is a good approach to do it: 利用路由按照在routes.rb中定义的顺序进行匹配的事实,这是一种很好的方法:

match '/websocket' => 'controller#action'
match '/:slug' => "errors#show", :code => 404, :via => [:get]

When a request /string comes, the routing subsystem will first try to match it to the first line, and if string is equal to websocket then the match is successful and no more routes will be matched. 当请求/string到来时,路由子系统将首先尝试将其与第一行匹配,如果string等于websocket则匹配成功,并且将不再匹配任何路由。

If string is not websocket on the other hand, then it will match the second line. 如果string不是websocket ,则它将与第二行匹配。

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

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