Skip to content

Commit f19e41f

Browse files
author
Francesco Rodriguez
committed
update guides to use _action callbacks [ci skip]
1 parent 69163cc commit f19e41f

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

guides/source/action_controller_overview.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Filters are inherited, so if you set a filter on `ApplicationController`, it wil
434434

435435
```ruby
436436
class ApplicationController < ActionController::Base
437-
before_filter :require_login
437+
before_action :require_login
438438

439439
private
440440

@@ -458,11 +458,11 @@ end
458458

459459
The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a "before" filter renders or redirects, the action will not run. If there are additional filters scheduled to run after that filter, they are also cancelled.
460460

461-
In this example the filter is added to `ApplicationController` and thus all controllers in the application inherit it. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with `skip_before_filter`:
461+
In this example the filter is added to `ApplicationController` and thus all controllers in the application inherit it. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with `skip_before_action`:
462462

463463
```ruby
464464
class LoginsController < ApplicationController
465-
skip_before_filter :require_login, only: [:new, :create]
465+
skip_before_action :require_login, only: [:new, :create]
466466
end
467467
```
468468

@@ -480,7 +480,7 @@ For example, in a website where changes have an approval workflow an administrat
480480

481481
```ruby
482482
class ChangesController < ActionController::Base
483-
around_filter :wrap_in_transaction, only: :show
483+
around_action :wrap_in_transaction, only: :show
484484

485485
private
486486

@@ -502,13 +502,13 @@ You can choose not to yield and build the response yourself, in which case the a
502502

503503
### Other Ways to Use Filters
504504

505-
While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.
505+
While the most common way to use filters is by creating private methods and using *_action to add them, there are two other ways to do the same thing.
506506

507-
The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the `require_login` filter from above could be rewritten to use a block:
507+
The first is to use a block directly with the *_action methods. The block receives the controller as an argument, and the `require_login` filter from above could be rewritten to use a block:
508508

509509
```ruby
510510
class ApplicationController < ActionController::Base
511-
before_filter do |controller|
511+
before_action do |controller|
512512
redirect_to new_login_url unless controller.send(:logged_in?)
513513
end
514514
end
@@ -520,7 +520,7 @@ The second way is to use a class (actually, any object that responds to the righ
520520

521521
```ruby
522522
class ApplicationController < ActionController::Base
523-
before_filter LoginFilter
523+
before_action LoginFilter
524524
end
525525

526526
class LoginFilter
@@ -648,7 +648,7 @@ HTTP digest authentication is superior to the basic authentication as it does no
648648
class AdminController < ApplicationController
649649
USERS = { "lifo" => "world" }
650650

651-
before_filter :authenticate
651+
before_action :authenticate
652652

653653
private
654654

@@ -828,7 +828,7 @@ end
828828

829829
class ClientsController < ApplicationController
830830
# Check that the user has the right authorization to access clients.
831-
before_filter :check_authorization
831+
before_action :check_authorization
832832

833833
# Note how the actions don't have to worry about all the auth stuff.
834834
def edit

guides/source/action_mailer_basics.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,17 @@ end
447447
Action Mailer Callbacks
448448
---------------------------
449449
450-
Action Mailer allows for you to specify a `before_filter`, `after_filter` and 'around_filter'.
450+
Action Mailer allows for you to specify a `before_action`, `after_action` and 'around_action'.
451451
452452
* Filters can be specified with a block or a symbol to a method in the mailer class similar to controllers.
453453
454-
* You could use a `before_filter` to prepopulate the mail object with defaults, delivery_method_options or insert default headers and attachments.
454+
* You could use a `before_action` to prepopulate the mail object with defaults, delivery_method_options or insert default headers and attachments.
455455
456-
* You could use an `after_filter` to do similar setup as a `before_filter` but using instance variables set in your mailer action.
456+
* You could use an `after_action` to do similar setup as a `before_action` but using instance variables set in your mailer action.
457457
458458
```ruby
459459
class UserMailer < ActionMailer::Base
460-
after_filter :set_delivery_options, :prevent_delivery_to_guests, :set_business_headers
460+
after_action :set_delivery_options, :prevent_delivery_to_guests, :set_business_headers
461461
462462
def feedback_message(business, user)
463463
@business = business

guides/source/action_view_overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ You can use the same technique to localize the rescue files in your public direc
14861486
Since Rails doesn't restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some "expert" users that should see different pages from "normal" users. You could add the following to `app/controllers/application.rb`:
14871487

14881488
```ruby
1489-
before_filter :set_expert_locale
1489+
before_action :set_expert_locale
14901490

14911491
def set_expert_locale
14921492
I18n.locale = :expert if current_user.expert?

guides/source/caching_with_rails.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Let's say you only wanted authenticated users to call actions on `ProductsContro
104104
```ruby
105105
class ProductsController < ActionController
106106

107-
before_filter :authenticate
107+
before_action :authenticate
108108
caches_action :index
109109

110110
def index

guides/source/i18n.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ However, you would probably like to **provide support for more locales** in your
134134

135135
WARNING: You may be tempted to store the chosen locale in a _session_ or a <em>cookie</em>, however **do not do this**. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being [<em>RESTful</em>](http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about the RESTful approach in [Stefan Tilkov's articles](http://www.infoq.com/articles/rest-introduction). Sometimes there are exceptions to this rule and those are discussed below.
136136

137-
The _setting part_ is easy. You can set the locale in a `before_filter` in the `ApplicationController` like this:
137+
The _setting part_ is easy. You can set the locale in a `before_action` in the `ApplicationController` like this:
138138

139139
```ruby
140-
before_filter :set_locale
140+
before_action :set_locale
141141

142142
def set_locale
143143
I18n.locale = params[:locale] || I18n.default_locale
@@ -160,7 +160,7 @@ One option you have is to set the locale from the domain name where your applica
160160
You can implement it like this in your `ApplicationController`:
161161

162162
```ruby
163-
before_filter :set_locale
163+
before_action :set_locale
164164

165165
def set_locale
166166
I18n.locale = extract_locale_from_tld || I18n.default_locale
@@ -203,7 +203,7 @@ This solution has aforementioned advantages, however, you may not be able or may
203203

204204
### Setting the Locale from the URL Params
205205

206-
The most usual way of setting (and passing) the locale would be to include it in URL params, as we did in the `I18n.locale = params[:locale]` _before_filter_ in the first example. We would like to have URLs like `www.example.com/books?locale=ja` or `www.example.com/ja/books` in this case.
206+
The most usual way of setting (and passing) the locale would be to include it in URL params, as we did in the `I18n.locale = params[:locale]` _before_action_ in the first example. We would like to have URLs like `www.example.com/books?locale=ja` or `www.example.com/ja/books` in this case.
207207

208208
This approach has almost the same set of advantages as setting the locale from the domain name: namely that it's RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.
209209

guides/source/security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ NOTE: _When sanitizing, protecting or verifying something, whitelists over black
688688

689689
A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although sometimes it is not possible to create a whitelist (in a SPAM filter, for example), _prefer to use whitelist approaches_:
690690

691-
* Use before_filter only: [...] instead of except: [...]. This way you don't forget to turn it off for newly added actions.
691+
* Use before_action only: [...] instead of except: [...]. This way you don't forget to turn it off for newly added actions.
692692
* Use attr_accessible instead of attr_protected. See the mass-assignment section for details
693693
* Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scripting (XSS). See below for details.
694694
* Don't try to correct user input by blacklists:

0 commit comments

Comments
 (0)