Rails 3.1 - in routes.rb, using the wildcard, match '*' is not catching any routes

爱⌒轻易说出口 提交于 2019-12-13 18:31:06

问题


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'

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.)

Troubleshooting, I put this in my routes.rb file:

match 'foobar' => 'pages#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'

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?


回答1:


Maybe try this :

match '*path' => 'pages#not_found'


来源:https://stackoverflow.com/questions/10380805/rails-3-1-in-routes-rb-using-the-wildcard-match-is-not-catching-any-rout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!