Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rescue from routing error rails 3.1

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.

like image 425
roman Avatar asked Mar 15 '12 15:03

roman


2 Answers

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
like image 163
gtd Avatar answered Oct 16 '22 01:10

gtd


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
like image 42
aboutaaron Avatar answered Oct 16 '22 02:10

aboutaaron