diff --git a/README.md b/README.md index a09580d..2f795dd 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_353 TILs and counting..._ +_354 TILs and counting..._ --- @@ -222,6 +222,7 @@ _353 TILs and counting..._ - [Migrating Up Down Up](rails/migrating-up-down-up.md) - [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md) - [Pretend Generations](rails/pretend-generations.md) +- [Rescue From](rails/rescue-from.md) - [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md) - [Select A Select By Selector](rails/select-a-select-by-selector.md) - [Select Value For SQL Counts](rails/select-value-for-sql-counts.md) diff --git a/rails/rescue-from.md b/rails/rescue-from.md new file mode 100644 index 0000000..0b92e8d --- /dev/null +++ b/rails/rescue-from.md @@ -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 +```