You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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".
Copy file name to clipboardExpand all lines: guides/source/action_controller_overview.md
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ After reading this guide, you will know how to:
10
10
* Follow the flow of a request through a controller.
11
11
* Restrict parameters passed to your controller.
12
12
* 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.
14
14
* Use Action Controller's built-in HTTP authentication.
15
15
* Stream data directly to the user's browser.
16
16
* 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
710
710
You can learn more about rendering in the [Layouts and Rendering
711
711
Guide](layouts_and_rendering.html).
712
712
713
-
Filters
714
-
-------
713
+
Action callbacks
714
+
----------------
715
715
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.
717
717
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.
719
719
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:
721
721
722
722
```ruby
723
723
class ApplicationController < ActionController::Base
@@ -733,31 +733,31 @@ class ApplicationController < ActionController::Base
733
733
end
734
734
```
735
735
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.
737
737
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`][]:
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.
747
747
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.
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.
757
757
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.
759
759
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.
761
761
762
762
For example, in a website where changes have an approval workflow, an administrator could preview them easily by applying them within a transaction:
763
763
@@ -778,18 +778,18 @@ class ChangesController < ApplicationController
778
778
end
779
779
```
780
780
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.
782
782
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.
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.
791
791
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:
793
793
794
794
```ruby
795
795
class ApplicationController < ActionController::Base
@@ -802,22 +802,22 @@ class ApplicationController < ActionController::Base
802
802
end
803
803
```
804
804
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.
806
806
807
807
Specifically for `around_action`, the block also yields in the `action`:
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:
814
814
815
815
```ruby
816
816
class ApplicationController < ActionController::Base
817
-
before_action LoginFilter
817
+
before_action LoginActionCallback
818
818
end
819
819
820
-
class LoginFilter
820
+
class LoginActionCallback
821
821
def self.before(controller)
822
822
unless controller.send(:logged_in?)
823
823
controller.flash[:error] = "You must be logged in to access this section"
@@ -827,7 +827,7 @@ class LoginFilter
827
827
end
828
828
```
829
829
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.
831
831
832
832
Request Forgery Protection
833
833
--------------------------
@@ -901,7 +901,7 @@ Rails collects all of the parameters sent along with the request in the `params`
901
901
902
902
### The `response` Object
903
903
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).
@@ -941,7 +941,7 @@ class AdminsController < ApplicationController
941
941
end
942
942
```
943
943
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.
0 commit comments