Dynamic error pages in Rails 3 Dynamic error pages in Rails 3 ruby-on-rails ruby-on-rails

Dynamic error pages in Rails 3


In rails 3.2, it's easier than that:

Add this to config/application.rb:

config.exceptions_app = self.routes

That causes errors to be routed via the router. Then you just add to config/routes.rb:

match "/404", :to => "errors#not_found"

I got this info from item #3 on the blog post "My five favorite hidden features in Rails 3.2" by By José Valim.


I would suggest using rescue_from instead. You would just rescue from specific errors rather than overriding rescue_action_in_public. This is especially useful when dealing with user-defined errors or controller-specific errors.

# ApplicationControllerrescue_from ActionController::RoutingError, :with => :render_404rescue_from ActionController::UnknownAction, :with => :render_404rescue_from ActiveRecord::RecordNotFound, :with => :render_404rescue_from MyApp::CustomError, :with => :custom_error_resolutiondef render_404  if /(jpe?g|png|gif)/i === request.path    render :text => "404 Not Found", :status => 404  else    render :template => "shared/404", :layout => 'application', :status => 404  endend# UsersControllerrescue_from MyApp::SomeReallySpecificUserError, :with => :user_controller_resolution

http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html


The exception notifier has a method called notify_about_exception to initiate the error notification on demand.

class ApplicationController < ActionController::Base  include ExceptionNotification::Notifiable  rescue_from Exception, :with => :render_all_errors  def render_all_errors(e)    log_error(e) # log the error    notify_about_exception(e) # send the error notification    # now handle the page    if e.is_a?(ActionController::RoutingError)      render_404(e)    else      render_other_error(e)    end  end  def render_404_error(e)   # your code  end  def render_other_error(e)   # your code  endend