Rails 3.1-在routes.rb中,使用通配符,匹配“ *”未捕获任何路由 [英] Rails 3.1 - in routes.rb, using the wildcard, match '*' is not catching any routes
问题描述
在我的 pages_controller.rb
文件中,我执行了 not_found
操作。我想让 routes.rb
文件中的条目不匹配的任何路由转到 pages#not_found
。要利用通配符,我将其设置为routes.rb文件的最后一行:
In my pages_controller.rb
file I have a not_found
action. I want to have any route that is not matched by an entry in my routes.rb
file to go to pages#not_found
. Wanting to leverage a wildcard, I made this the last line of my routes.rb file:
match '*' => 'pages#not_found'
我将其基于我在ruby指南中对通配符的了解路由(http://guides.rubyonrails.org/v3.1.0/routing.html)。但是,它不起作用。而不是转到 pages#not_found
,我得到: AcctionController :: RoutingError(没有路由与[GET] *
(不幸的是,ActionDispatch捕获了它,所以我没有办法处理异常。)
I'm basing this on what I read about wildcards in the ruby guide for routing (http://guides.rubyonrails.org/v3.1.0/routing.html). However it is not working. Instead of going to pages#not_found
, I get: AcctionController::RoutingError (No route matches [GET] "*"
(which is unfortunately caught by ActionDispatch so I don't have a way to handle the exception.)
问题排查,我把它放在了routes.rb文件中:
Troubleshooting, I put this in my routes.rb file:
match 'foobar' => 'pages#not_found'
按预期方式,在 http:// localhost:3000 / foobar
上执行了not_found操作。卡:
the not_found action was executed on http://localhost:3000/foobar
as expected. I tried a partial wild card:
match 'foo*' => 'pages#not_found'
根据Rails指南,这应该与 http :// localhost:3000 / foobar 但这不是。您需要做一些特殊的事情来使通配符在路由中工作吗?我想念什么?
According to the Rails guide, this should match to http://localhost:3000/foobar but it doesn't. Is there something special that you need to do to get wildcards to work in routes? What am I missing?
推荐答案
也许可以尝试一下:
match '*path' => 'pages#not_found'
这篇关于Rails 3.1-在routes.rb中,使用通配符,匹配“ *”未捕获任何路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!