Skip to content

Commit

Permalink
Remove custom errors page section from the guides
Browse files Browse the repository at this point in the history
This pattern is too problematic and introduces a lot of edge cases:

  * On 4.2, the issue #15124 is
    back again.
  * needs to define each action for each http status otherwise the
    router raises ActionController::RoutingError (No route matches).
  * If the router has `match "/*username",...` and some action is
    missing, Rails will pick up the "match" and try to do its job.
  * encourages people to copy & paste programming. Not DRY.

[ci skip]
  • Loading branch information
yuki24 committed Nov 28, 2014
1 parent f25ad07 commit cbcec99
Showing 1 changed file with 1 addition and 58 deletions.
59 changes: 1 addition & 58 deletions guides/source/action_controller_overview.md
Expand Up @@ -1164,67 +1164,10 @@ class ClientsController < ApplicationController
end
```

WARNING: You shouldn't do `rescue_from Exception` or `rescue_from StandardError` unless you have a particular reason as it will cause serious side-effects (e.g. you won't be able to see exception details and tracebacks during development). If you would like to dynamically generate error pages, see [Custom errors page](#custom-errors-page).
WARNING: You shouldn't do `rescue_from Exception` or `rescue_from StandardError` unless you have a particular reason as it will cause serious side-effects (e.g. you won't be able to see exception details and tracebacks during development).

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
match '/404', via: :all, to: 'errors#not_found'
match '/422', via: :all, to: 'errors#unprocessable_entity'
match '/500', via: :all, to: 'errors#server_error'
```

Create the controller and views.

* `app/controllers/errors_controller.rb`

```ruby
class ErrorsController < ActionController::Base
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.

WARNING: 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 like presenting an empty page for the users.

Force HTTPS protocol
--------------------

Expand Down

0 comments on commit cbcec99

Please sign in to comment.