在我的pages_controller.rb
文件中,我有一个not_found
动作。我想要任何与我的routes.rb
文件中的条目不匹配的路线去pages#not_found
。为了利用通配符,我将其作为 routes.rb 文件的最后一行:
match '*' => 'pages#not_found'
我是基于我在 ruby 路由指南 (http://guides.rubyonrails.org/v3.1.0/routing.html) 中读到的关于通配符的内容。但是它不起作用。我没有去pages#not_found
,而是得到:(AcctionController::RoutingError (No route matches [GET] "*"
不幸的是,它被 ActionDispatch 捕获了,所以我没有办法处理异常。)
故障排除,我把它放在我的 routes.rb 文件中:
match 'foobar' => 'pages#not_found'
not_found 操作http://localhost:3000/foobar
按预期执行。我尝试了部分通配符:
match 'foo*' => 'pages#not_found'
根据 Rails 指南,这应该与http://localhost:3000/foobar匹配,但事实并非如此。您是否需要做一些特别的事情才能让通配符在路线中起作用?我错过了什么?