diff options
author | Eileen M. Uchitelle <eileencodes@gmail.com> | 2014-06-19 14:39:23 -0400 |
---|---|---|
committer | Eileen M. Uchitelle <eileencodes@gmail.com> | 2014-06-19 14:39:23 -0400 |
commit | c36e77a93e286f4546a36e79d43f543e9f00d6be (patch) | |
tree | c39efe1ef8ec047fef42b8f9b8a31c6f9fb9d668 | |
parent | 27953bc267d4cf8c9588c8e128fb1f7df445c92a (diff) | |
parent | 32e83e7d8d5a9e576c00153edfc6c092e1b50e38 (diff) | |
download | rails-c36e77a93e286f4546a36e79d43f543e9f00d6be.tar.gz rails-c36e77a93e286f4546a36e79d43f543e9f00d6be.tar.bz2 rails-c36e77a93e286f4546a36e79d43f543e9f00d6be.zip |
Merge pull request #15808 from maurogeorge/guides-custom-errors-page
Create custom errors page on ActionController guides
-rw-r--r-- | guides/source/action_controller_overview.md | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/guides/source/action_controller_overview.md b/guides/source/action_controller_overview.md index 3d15319ca4..0d1bd2061d 100644 --- a/guides/source/action_controller_overview.md +++ b/guides/source/action_controller_overview.md @@ -1166,6 +1166,61 @@ end NOTE: Certain exceptions are only rescuable from the `ApplicationController` class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's [article](http://m.onkey.org/2008/7/20/rescue-from-dispatching) on the subject for more information. + +### Custom errors page + +You can customize the layout of your error handling using controllers and views. +First define your app own routes to display the errors page. + +* `config/application.rb` + + ```ruby + config.exceptions_app = self.routes + ``` + +* `config/routes.rb` + + ```ruby + get '/404', to: 'errors#not_found' + get '/422', to: 'errors#unprocessable_entity' + get '/500', to: 'errors#server_error' + ``` + +Create the controller and views. + +* `app/controllers/errors_controller.rb` + + ```ruby + class ErrorsController < ApplicationController + layout 'error' + + def not_found + render status: :not_found + end + + def unprocessable_entity + render status: :unprocessable_entity + end + + def server_error + render status: :server_error + end + end + ``` + +* `app/views` + + ``` + errors/ + not_found.html.erb + unprocessable_entity.html.erb + server_error.html.erb + layouts/ + error.html.erb + ``` + +Do not forget to set the correct status code on the controller as shown before. You should avoid using the database or any complex operations because the user is already on the error page. Generating another error while on an error page could cause issues. + Force HTTPS protocol -------------------- |