views:

2039

answers:

5

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.

A: 

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.

Ben Alpert
A: 

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.

theIV
+4  A: 

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.

Leonid Shevtsov
Thanks for the answer, I also wanted to add that there is an indepth writeup that I found on this topichttp://www.perfectline.co.uk/blog/custom-dynamic-error-pages-in-ruby-on-rails
ThinkBohemian
A: 

yes but the 404 will not be served to the user by default. You have to do what leonid said otherwise you get a routing error message which does not look pretty at all.

mike
A: 

This answer does not work, at least on Edge.

ACK
Have a better answer?
ChrisH