Basic Rails 404 Error Page

chrishomer picture chrishomer · Sep 19, 2009 · Viewed 23.6k times · Source

I have been looking for a simple answer to this for a ridiculously long time and it seems like this has to be so plainly obvious and simple because no one has an easy, idiot proof tutorial.

Anyway, all I want to do is to have a single 404.html static page that loads whenever ANY error is thrown. Ideally this should only happen in production and staging.

I feel like this should be the easiest thing to do... but I can't figure it out.

Any help is much appreciated.

Answer

Leonid Shevtsov picture Leonid Shevtsov · Sep 19, 2009

in your ApplicationController

unless  ActionController::Base.consider_all_requests_local
  rescue_from Exception, :with => :render_404
end

private

  def render_404
    render :template => 'error_pages/404', :layout => false, :status => :not_found
  end

now set up error_pages/404.html and there you go

...or maybe I'm overcautious with Exception and you should rescue from RuntimeError instead.