Skip to content

Commit 1d13a7c

Browse files
Merge pull request rails#51645 from notapatch/pr-guides-acion-controller-action-callbacks
Update guides: Replace filter with action callback [ci skip] Rails 4.2 onwards replaced filter with action callback. However, the term has remained in the rails guides and this PR fixes this. Types of change: * Filter(s) => Action callback(s) * Generalized the word "Action callbacks" as it is longer. * actions => "controller actions" to make "controller actions" more distinct from "action callbacks".
2 parents 185157f + 66bc210 commit 1d13a7c

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

guides/source/action_controller_overview.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ After reading this guide, you will know how to:
1010
* Follow the flow of a request through a controller.
1111
* Restrict parameters passed to your controller.
1212
* Store data in the session or cookies, and why.
13-
* Work with filters to execute code during request processing.
13+
* Work with action callbacks to execute code during request processing.
1414
* Use Action Controller's built-in HTTP authentication.
1515
* Stream data directly to the user's browser.
1616
* Filter sensitive parameters, so they do not appear in the application's log.
@@ -710,14 +710,14 @@ You may notice in the above code that we're using `render xml: @users`, not `ren
710710
You can learn more about rendering in the [Layouts and Rendering
711711
Guide](layouts_and_rendering.html).
712712

713-
Filters
714-
-------
713+
Action callbacks
714+
----------------
715715

716-
Filters are methods that are run "before", "after" or "around" a controller action.
716+
Action callbacks are methods that are run "before", "after" or "around" a controller action.
717717

718-
Filters are inherited, so if you set a filter on `ApplicationController`, it will be run on every controller in your application.
718+
Action callbacks are inherited, so if you set one on an `ApplicationController`, it will be run on every controller in your application.
719719

720-
"before" filters are registered via [`before_action`][]. They may halt the request cycle. A common "before" filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:
720+
"before" action callbacks are registered via [`before_action`][]. They may halt the request cycle. A common "before" action callback is one which requires that a user is logged in for an action to be run. You can define the method this way:
721721

722722
```ruby
723723
class ApplicationController < ActionController::Base
@@ -733,31 +733,31 @@ class ApplicationController < ActionController::Base
733733
end
734734
```
735735

736-
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.
736+
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" action callback renders or redirects, the controller action will not run. If there are additional action callbacks scheduled to run after that one, they are also cancelled.
737737

738-
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 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`][]:
738+
In this example, the action callback 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 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 action callback from running before particular actions with [`skip_before_action`][]:
739739

740740
```ruby
741741
class LoginsController < ApplicationController
742742
skip_before_action :require_login, only: [:new, :create]
743743
end
744744
```
745745

746-
Now, the `LoginsController`'s `new` and `create` actions will work as before without requiring the user to be logged in. The `:only` option is used to skip this filter only for these actions, and there is also an `:except` option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.
746+
Now, the `LoginsController`'s `new` and `create` actions will work as before without requiring the user to be logged in. The `:only` option is used to skip this action callback only for these actions, and there is also an `:except` option which works the other way. These options can be used when adding action callbacks too, so you can add a callback which only runs for selected actions in the first place.
747747

748-
NOTE: Calling the same filter multiple times with different options will not work,
749-
since the last filter definition will overwrite the previous ones.
748+
NOTE: Calling the same action callback multiple times with different options will not work,
749+
since the last action callback definition will overwrite the previous ones.
750750

751751
[`before_action`]: https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-before_action
752752
[`skip_before_action`]: https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-skip_before_action
753753

754-
### After Filters and Around Filters
754+
### After Action and Around Action Callbacks
755755

756-
In addition to "before" filters, you can also run filters after an action has been executed, or both before and after.
756+
In addition to "before" action callback, you can also run action callbacks after a controller action has been executed, or both before and after.
757757

758-
"after" filters are registered via [`after_action`][]. They are similar to "before" filters, but because the action has already been run they have access to the response data that's about to be sent to the client. Obviously, "after" filters cannot stop the action from running. Please note that "after" filters are executed only after a successful action, but not when an exception is raised in the request cycle.
758+
"after" action callbacks are registered via [`after_action`][]. They are similar to "before" action callbacks, but because the controller action has already been run they have access to the response data that's about to be sent to the client. Obviously, "after" action callbacks cannot stop the action from running. Please note that "after" action callbacks are executed only after a successful controller action, but not when an exception is raised in the request cycle.
759759

760-
"around" filters are registered via [`around_action`][]. They are responsible for running their associated actions by yielding, similar to how Rack middlewares work.
760+
"around" action callbacks are registered via [`around_action`][]. They are responsible for running their associated actions by yielding, similar to how Rack middlewares work.
761761

762762
For example, in a website where changes have an approval workflow, an administrator could preview them easily by applying them within a transaction:
763763

@@ -778,18 +778,18 @@ class ChangesController < ApplicationController
778778
end
779779
```
780780

781-
Note that an "around" filter also wraps rendering. In particular, in the example above, if the view itself reads from the database (e.g. via a scope), it will do so within the transaction and thus present the data to preview.
781+
Note that an "around" action callback also wraps rendering. In particular, in the example above, if the view itself reads from the database (e.g. via a scope), it will do so within the transaction and thus present the data to preview.
782782

783-
You can choose not to yield and build the response yourself, in which case the action will not be run.
783+
You can choose not to yield and build the response yourself, in which case the controller action will not be run.
784784

785785
[`after_action`]: https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-after_action
786786
[`around_action`]: https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-around_action
787787

788-
### Other Ways to Use Filters
788+
### Other Ways to Use Action Callbacks
789789

790-
While the most common way to use filters is by creating private methods and using `before_action`, `after_action`, or `around_action` to add them, there are two other ways to do the same thing.
790+
While the most common way to use action callbacks is by creating private methods and using `before_action`, `after_action`, or `around_action` to add them, there are two other ways to do the same thing.
791791

792-
The first is to use a block directly with the `*_action` methods. The block receives the controller as an argument. The `require_login` filter from above could be rewritten to use a block:
792+
The first is to use a block directly with the `*_action` methods. The block receives the controller as an argument. The `require_login` action callback from above could be rewritten to use a block:
793793

794794
```ruby
795795
class ApplicationController < ActionController::Base
@@ -802,22 +802,22 @@ class ApplicationController < ActionController::Base
802802
end
803803
```
804804

805-
Note that the filter, in this case, uses `send` because the `logged_in?` method is private, and the filter does not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in simpler cases, it might be useful.
805+
Note that the action callback, in this case, uses `send` because the `logged_in?` method is private, and the action callback does not run in the scope of the controller. This is not the recommended way to implement this particular action callback, but in simpler cases, it might be useful.
806806

807807
Specifically for `around_action`, the block also yields in the `action`:
808808

809809
```ruby
810810
around_action { |_controller, action| time(&action) }
811811
```
812812

813-
The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex and cannot be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
813+
The second way is to use a class (actually, any object that responds to the right methods will do) to handle the callback action. This is useful in cases that are more complex and cannot be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login action callback again to use a class:
814814

815815
```ruby
816816
class ApplicationController < ActionController::Base
817-
before_action LoginFilter
817+
before_action LoginActionCallback
818818
end
819819
820-
class LoginFilter
820+
class LoginActionCallback
821821
def self.before(controller)
822822
unless controller.send(:logged_in?)
823823
controller.flash[:error] = "You must be logged in to access this section"
@@ -827,7 +827,7 @@ class LoginFilter
827827
end
828828
```
829829

830-
Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class must implement a method with the same name as the filter, so for the `before_action` filter, the class must implement a `before` method, and so on. The `around` method must `yield` to execute the action.
830+
Again, this is not an ideal example for this action callback, because it's not run in the scope of the controller but gets the controller passed as an argument. The class must implement a method with the same name as the action callback, so for the `before_action` action callback, the class must implement a `before` method, and so on. The `around` method must `yield` to execute the action.
831831

832832
Request Forgery Protection
833833
--------------------------
@@ -901,7 +901,7 @@ Rails collects all of the parameters sent along with the request in the `params`
901901

902902
### The `response` Object
903903

904-
The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values. To get a full list of the available methods, refer to the [Rails API documentation](https://api.rubyonrails.org/classes/ActionDispatch/Response.html) and [Rack Documentation](https://www.rubydoc.info/github/rack/rack/Rack/Response).
904+
The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after action callback - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values. To get a full list of the available methods, refer to the [Rails API documentation](https://api.rubyonrails.org/classes/ActionDispatch/Response.html) and [Rack Documentation](https://www.rubydoc.info/github/rack/rack/Rack/Response).
905905

906906
| Property of `response` | Purpose |
907907
| ---------------------- | --------------------------------------------------------------------------------------------------- |
@@ -941,7 +941,7 @@ class AdminsController < ApplicationController
941941
end
942942
```
943943

944-
With this in place, you can create namespaced controllers that inherit from `AdminsController`. The filter will thus be run for all actions in those controllers, protecting them with HTTP basic authentication.
944+
With this in place, you can create namespaced controllers that inherit from `AdminsController`. The action callback will thus be run for all actions in those controllers, protecting them with HTTP basic authentication.
945945

946946
[`http_basic_authenticate_with`]: https://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic/ControllerMethods/ClassMethods.html#method-i-http_basic_authenticate_with
947947

0 commit comments

Comments
 (0)