Reputation: 5210
How to rescue from RoutingError
in rails 3.1 application. If i'm nt mistaken it was possible to use rescue_from RoutingError
in application controller but now it's not possible.
Upvotes: 7
Views: 4694
Reputation: 13036
Good example.
route.rb
Rails 3:
match '*unmatched_route', :to => 'application#raise_not_found!'
Rails 4:
get '*unmatched_route' => 'application#raise_not_found!'
application_controller.rb
def raise_not_found!
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end
Upvotes: 0
Reputation: 5389
I wasn't able to replicate @matthew-savage's results. However, per the Rails guide on route globbing and this question on another StackOverflow question, I solved this issue like so:
routes.rb
match "*gibberish", :to => "home#routing_error"
notice how I included text after the wildcard. The controller is fine as shown above:
controller/home_controller.rb
....
def routing_error
render text: "Not found, sorry", status: :not_found
end
Upvotes: 7
Reputation: 17246
There is no great way to handle it, but there are a few workarounds. The discussion here yields the following suggestion:
Routes
Add the following to your routes file:
match "*", :to => "home#routing_error"
and handle the error in this action:
def routing_error
render text: "Not found, sorry", status: :not_found
end
Upvotes: 7