提交 c36e77a9 编写于 作者: E Eileen M. Uchitelle

Merge pull request #15808 from maurogeorge/guides-custom-errors-page

Create custom errors page on ActionController guides
......@@ -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
--------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册