Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Rails: Skipping Base Class Filters in Subclasses

with 3 comments

I encountered a case where we needed to skip a filter as it was causing infinite redirection in a subclass. I found my answer in: Rails Filters Documentation

Filter chain skipping

Declaring a filter on a base class conveniently applies to its subclasses, but sometimes a subclass should skip some of its superclass’ filters:

  class ApplicationController < ActionController::Base
    before_filter :authenticate
    around_filter :catch_exceptions
  end

  class WeblogController < ApplicationController
    # Will run the :authenticate and :catch_exceptions filters.
  end

  class SignupController < ApplicationController
    # Skip :authenticate, run :catch_exceptions.
    skip_before_filter :authenticate
  end

  class ProjectsController < ApplicationController
    # Skip :catch_exceptions, run :authenticate.
    skip_filter :catch_exceptions
  end

  class ClientsController  :index
  end
Advertisements

Written by imsaar

November 10, 2009 at 9:07 pm

Posted in code, documentation, rails, ruby

3 Responses

Subscribe to comments with RSS.

  1. You can also use the :except option in filters – all the filter methods take in :include and :exclude options (e.g. when you don’t want the filter to be executed for a certain actions).

    arnab

    November 11, 2009 at 12:01 am

    • As far as I understand :except only takes an aray of methods in the current controller. So you have to define the filter in the subclass that is already defined in the parent controller and then add the except clause at the end of it. Is that a correct understanding?

      imsaar

      November 11, 2009 at 12:09 am

      • You don’t have to define it again – inheritence will take care of that. So it’ll be like:

        class ApplicationController < ActionController::Base
        before_filter :authenticate
        around_filter :catch_exceptions
        end

        class ProjectsController ‘some_action’
        end

        arnab

        November 11, 2009 at 1:46 am


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

%d bloggers like this: