Handling Routing ERROR in Rails 3.2

Routing Errors are caught if somebody is calling a path on your app which is not existing. This how I handled it. I just added this line to the end of the routes.rb

match '*path', :to => 'page#routing_error'

It is important that this is on the bottom of the routes.rb. That just means, if no other route matches than handle that by the PageController and the routing_error Action. That can look like this:

class PageController < ApplicationController

  def routing_error
    p "routing error path: #{params[:path]}"
    redirect_to "/"
  end

end

3 thoughts on “Handling Routing ERROR in Rails 3.2

  1. If you want to still see the error message in development, add this to you render_not_found method:

    if Rails.application.config.consider_all_requests_local
    raise ActionController::RoutingError.new(‘Not Found’)
    end

  2. Although this is a very common approach to catch missing routes, certain routes which are generated by gems. Putting a route which catches all such routes which are not present at the end of the route might also interfere with those routes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s