aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/actioncontroller/filters.txt
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-10-16 22:13:06 +0200
committerPratik Naik <pratiknaik@gmail.com>2008-10-16 22:13:06 +0200
commit9cb5400871b660e2c6d1654346650f07bb52a0c0 (patch)
tree6cd292650cf80b25494cf2f800318f337517b732 /railties/doc/guides/actioncontroller/filters.txt
parent517bc500ed95a84fd2aadff34fdc14cb7965bc6b (diff)
downloadrails-9cb5400871b660e2c6d1654346650f07bb52a0c0.tar.gz
rails-9cb5400871b660e2c6d1654346650f07bb52a0c0.tar.bz2
rails-9cb5400871b660e2c6d1654346650f07bb52a0c0.zip
Merge docrails
Diffstat (limited to 'railties/doc/guides/actioncontroller/filters.txt')
-rw-r--r--railties/doc/guides/actioncontroller/filters.txt23
1 files changed, 13 insertions, 10 deletions
diff --git a/railties/doc/guides/actioncontroller/filters.txt b/railties/doc/guides/actioncontroller/filters.txt
index 2baf92d6ef..a7b8d9727f 100644
--- a/railties/doc/guides/actioncontroller/filters.txt
+++ b/railties/doc/guides/actioncontroller/filters.txt
@@ -27,7 +27,7 @@ private
end
---------------------------------
-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 (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering/redirecting filter, they are also cancelled. To use this filter in a controller, use the "before_filter":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000704 method:
+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 (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering/redirecting filter, they are also cancelled. To use this filter in a controller, use the link:http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000704[before_filter] method:
[source, ruby]
---------------------------------
@@ -38,7 +38,7 @@ class ApplicationController < ActionController::Base
end
---------------------------------
-In this example, the filter is added to ApplicationController and thus all controllers in the application. 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, so to prevent this filter from running you can use "skip_before_filter":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000711 :
+In this example, the filter is added to ApplicationController and thus all controllers in the application. 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, so to prevent this filter from running you can use link:http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000711[skip_before_filter] :
[source, ruby]
---------------------------------
@@ -59,24 +59,27 @@ TODO: Find a real example for an around filter
[source, ruby]
---------------------------------
+# Example taken from the Rails API filter documentation:
+# http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
class ApplicationController < Application
- around_filter :foo
+ around_filter :catch_exceptions
private
- def foo
- logger.debug("Action has not been run yet")
- yield #Run the action
- logger.debug("Action has been run")
+ def catch_exceptions
+ yield
+ rescue => exception
+ logger.debug "Caught exception! #{exception}"
+ raise
end
end
---------------------------------
-=== Other types of filters ===
+=== Other ways to use filters ===
-While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways.
+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.
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 rewritte to use a block:
@@ -115,4 +118,4 @@ end
Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets it passed as an argument. The filter class has a class method `filter` which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same `filter` method, which will get run in the same way. The method must `yield` to execute the action. Alternatively, it can have both a `before` and an `after` method that are run before and after the action.
-The Rails API documentation has "more information and detail on using filters":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
+The Rails API documentation has link:http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html[more information on using filters].