问题
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