Rails: Capture RoutingError

Another small issue I came across this weekend is when I type in a bogus URL in my rails app.  i.e. http://localhost:3000/asdf

routes.rb maps routes for a Rails app and they are checked in order.  So if we add a route at the end of that list it will assume that the route didn’t match any previous route. Here’s a sample:

map.connect ‘*some_attribute’, :controller => application

Any route that hits this will be sent to the application/index. Here is  simple fix for the index method:

def index
logger.error exception.backtrace.join(”\n”)
render :template => “my_error_page”, :status => :not_found
end

2 Responses to “Rails: Capture RoutingError”

  1. Randy Says:

    For me, it followed the route to application#index, but it throws a NameError exception “undefined local variable or method `exception` for #

  2. wpdavenport Says:

    Thanks Randy. It seems that some formatting was removed when i posted. Let me try it again.

    At the very bottom of routes.rb paste:
    map.connect ‘/*some_attribute’, :controller => ‘application’

    You don’t need the logger in the application.rb file. But you will need to have a page called my_error_page.html.erb.

Leave a Reply