Rails Filters: Before, After, and Around Filters


Filters are methods that are run before, after or “around” a controller action.

Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.

Before filters 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.

Before filters are run on requests before the request gets to the controller’s action. It can return a response itself and completely bypass the action.

The most common use of before filters is validating a user’s authentication before granting them access to the action designated to handle their request. I’ve also seen them used to load a resource from the database, check permissions on a resource, or manage redirects under other circumstances.

After filters are run after the action completes. It can modify the response. Most of the time if something is done in an after filter, it can be done in the action itself, but if there is some logic to be run after running any of a set of actions, then an after filter is a good place to do it.

Generally, I’ve seen after and around filters used for logging.

Around filters may have logic before and after the action being run. It simply yields to the action in whatever place is necessary. Note that it doesn’t need to yield to the action and may run without doing so like a before filter.

You can use around filters for exception handling, setup and teardown, and a myriad of other cases.

Advertisements

6 thoughts on “Rails Filters: Before, After, and Around Filters

  1. Pravin, thats a good explanation. I think preference is towards before_action, after_action, around_action from rails 4.

    a) before_action filter example is allowing the user to sign in for any controller actions available in your app. Example snippet below

    class ApplicationController < ActionController::Base
    before_action :require_user # runs for all the controller actions

    private

    def require_user
    unless log_in?
    flash[:alert] = "Please login"
    redirect_to new_login_url # halts request cycle
    end
    end
    end

    b) after_action
    As this runs after the action, you have response object available to get manipulated. We can perform any sort of operation before displaying it to user.
    class ApplicationController < ActionController::Base
    after_action :manipulate_response # runs for all the controller actions in the app

    private

    def manipulate_response
    // Code goes here
    end
    end

    c) around_action (Reference: http://guides.rubyonrails.org/action_controller_overview.html)

    class ChangesController < ApplicationController
    around_action :wrap_in_transaction, only: :show

    private

    def wrap_in_transaction
    ActiveRecord::Base.transaction do
    begin
    yield
    ensure
    raise ActiveRecord::Rollback
    end
    end
    end
    end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s