1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add Rescue From as a rails til

This commit is contained in:
jbranchaud
2016-03-03 21:24:44 -06:00
parent b8453eff1b
commit 3eaac3a962
2 changed files with 23 additions and 1 deletions

21
rails/rescue-from.md Normal file
View File

@@ -0,0 +1,21 @@
# Rescue From
The
[`rescue_from`](http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html)
method, provided by `ActiveSupport`, is a handy way to provide a catch-all
response to a particular exception.
For instance, if many of the controllers in your application raise a
`User::NotAuthorized` error for unauthorized requests, the
`ApplicationController` can provide a unified response. This will help dry
up your controllers and prevent any potential inconsistencies.
```ruby
class ApplicationController < ActionController::Base
rescue_from User::NotAuthorized do |exception|
# respond with some Not Authorized page
end
...
end
```