How to handle errors like 404/500 in rails3

Hey, hope you can help me.

I am trying to find a way to direct the user to the default 404.html and 500.html error pages in my shared folder.

So, when there is a routing or nomethod error, it should be directed there. I already tried some things in my application controller, but it did not work.

Many thanks!

+7
source share
3 answers

Rails does this for you automatically when you start in production mode. When you download the application to a live server, Rails takes care of handling these exceptions and displays the correct error pages with the correct header status. If you are trying to see what these pages look like (for testing or something else), just access them directly through http://localhost:3000/404.html

Whenever you configure a Rails application on a real server (let it use Apache as an example), you provide the site root as the /public folder in your application. Then, whenever a request is made to the address of this server, Apache first looks at this shared folder and tries to service the static asset (this is a custom parameter in [environment].rb ). If it cannot find the requested page, the request is sent through the Ruby stack.

In production mode, if Rails detects an error that is not being processed (for example, start, save), it gives an error all the way to the stack, which then tells Apache (again, in my example) to make the corresponding error.

Here are some common errors that you will see in development mode and what they display in production mode:

 ActiveRecord::RecordNotFound => 404 (page not found) nil.method => 500 (server error) unless you turn off whiny nils ActionController::RoutingError => 404 (page not found) 
+19
source

It is executed automatically at startup in production mode - you do not need to do this manually.

+2
source

Check out this post to redirect all requests that cause a routing error.

+2
source

All Articles