Skip before_filter in Rails

Ruby on-RailsInheritanceBefore Filter

Ruby on-Rails Problem Overview


Names and objects have been simplified for clarity's sake. The basic concept remains the same.

I have three controllers: dog, cat, and horse. These controllers all inherit from the controller animal. In the controller animal, I have a before filter that authenticates a user as such:

before_filter :authenticate

def authenticate
  authenticate_or_request_with_http_basic do |name, password|
    name == "foo" && password == "bar"
  end
end

In the show action of dog, I need to have open access to all users (skip the authentication).

If I were to write the authentication separately for dog, I could do something like this:

before_filter :authenticate, :except => :show

But since dog inherits from animal, I do not have access to the controller-specific actions. Adding :except => :show in the animal controller will not only skip authentication for the show action of dog, but also that of cat and horse. This behaviour is not desired.

How can I skip the authentication only for the show action of dog while still inheriting from animal?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

class Dog < Animal
  skip_before_filter :authenticate, :only => :show
end

See ActionController::Filters::ClassMethods for more info on filters and inheritance.

Solution 2 - Ruby on-Rails

The two answers given are half right. In order to avoid making all your dog actions open, you need to qualify the skip_before_filter to only apply to the 'show' action as follows:

class Dog < Animal
  skip_before_filter :authenticate, :only => :show
end

Solution 3 - Ruby on-Rails

For this you can use skip_before_filter

It's explained in the Rails API

In your example dogjust would have to contain

skip_before_filter :authenticate

Solution 4 - Ruby on-Rails

Just a small update that using rails 4, it is now skip_before_action :authenticate, :only => :show and that before_filters should now use before_action instead.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuval KarmiView Question on Stackoverflow
Solution 1 - Ruby on-RailsJimmyView Answer on Stackoverflow
Solution 2 - Ruby on-RailsrigytView Answer on Stackoverflow
Solution 3 - Ruby on-RailsajmurmannView Answer on Stackoverflow
Solution 4 - Ruby on-RailsThreshView Answer on Stackoverflow