Take the 2-minute tour ×

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.

share|improve this question
add comment

4 Answers

up vote 15 down vote accepted

in your ApplicationController

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


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

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.

share|improve this answer
Thanks for the answer, I also wanted to add that there is an indepth writeup that I found on this topic perfectline.co.uk/blog/… –  Schneems Jan 16 '10 at 18:34
Doesn't seem to work on Rails 3 –  hoyhoy Feb 15 '11 at 21:57
it should work, though the documentation says you should use Rails.application.config.consider_all_requests_local instead of ActionController::Base.consider_all_requests_local. –  Leonid Shevtsov Feb 16 '11 at 15:11
Any errors in your app will return 404 now, not 500 as is proper though... And routing errors won't be rescued see rails.lighthouseapp.com/projects/8994/tickets/… Looking though I guess that IS what he asked, though it really shouldn't be done :) I use an nginx rule to throw 404 for anything in /images that can't be found. –  Kevin Apr 28 '11 at 14:43
add comment

I believe that if you run in production mode, then 404.html in the public directory gets served whenever there are no routes for a URL.

share|improve this answer
add comment

If you run in production mode, 404.html,500.html,422.html files in public directory gets served whenever there respective error occured, pages from above will be shown.

In rails 3.1

We can use like below: Rails 3.1 will automatically generate a response with the correct HTTP status code (in most cases, this is 200 OK). You can use the :status option to change this:

render :status => 500

render :status => :forbidden

Rails understands both numeric and symbolic status codes.

Fore more information see this page


share|improve this answer
add comment

You won't get a 404 whenever any error is thrown because not all errors result in 404s. That's why you have 404, 422, and 500 pages in your public directory. I guess rails has deemed these to be the most common errors. Like Ben said, 404 will come up when it can't find something, 500 when the application throws an error. Between the two, you can cover a lot of your bases.

share|improve this answer
add comment

Your Answer


