Spyros
Spyros

Reputation: 48686

How to skip a before_filter for Devise's SessionsController?

I have a before_filter in my ApplicationController; that is, for every controller in my project.

How can I skip_before_filter for Devise's SessionsController create action ?

Upvotes: 22

Views: 18081

Answers (7)

Kamil Sarna
Kamil Sarna

Reputation: 6301

I recently had this problem with filter in my application_controller I solved it using skip_before_filter

Rails < 4

skip_before_filter :check_subdomain!, if: :devise_controller?

Rails >= 4

skip_before_action :check_subdomain!, if: :devise_controller?

Upvotes: 26

Muhammad
Muhammad

Reputation: 730

You can simply check in your filter method whether it is devise controller or not.

  if params[:controller] != 'devise/sessions'

Upvotes: 1

user1186625
user1186625

Reputation:

Here's another way in lib/devise_sessions_controller_decorator.rb:

module DeviseSessionsControllerDecorator
  extend ActiveSupport::Concern

  included do
    skip_before_filter :your_filter_name
  end
end

Devise::SessionsController.send(:include, DeviseSessionsControllerDecorator)

Because classes are not cached in development mode, you may need to add something like this to config/environments/development.rb:

config.to_prepare do
  Devise::SessionsController.send(:include, DeviseSessionsControllerDecorator)
end

Upvotes: 2

Nathan Long
Nathan Long

Reputation: 126072

Before my colleague showed me the way I posted in my other answer, I did this. I'm posting this in case you think it's simpler.

class ApplicationController < ActionController::Base
  # ...
  before_filter :do_something

  def do_something
    unless params[:controller] == 'devise/sessions'
      # ...
    end
  end
end

Upvotes: 1

Nathan Long
Nathan Long

Reputation: 126072

Here's a method my colleague just showed me:

# In config/application.rb
module YourAppNameHere
  class Application < Rails::Application
  # Whatever else is already here...

    # The part to add
    config.to_prepare do
      Devise::SessionsController.skip_before_filter :your_before_filter_here
    end
  end
end

Upvotes: 65

Bert Goethals
Bert Goethals

Reputation: 7887

We did something like this:

First up, create your own session controller, make sure to inherit correctly:

class SessionsController < Devise::SessionsController
  skip_before_filter :foobar

Then fix the routes

devise_for :users,
  :controllers => {
    :sessions => "sessions"
  }

Alternatively you could monkey-patch Devise's session controller.

Upvotes: 16

Paul Kaplan
Paul Kaplan

Reputation: 2885

new answer

what about wrapping the before_filter in an unless block filtering by params[:controller]

def some_before_action
  unless params[:controller] == "sessions_controller_for_devise_name"
      ... #=> do the stuff here
  end 
end

old answer

just authorize which actions should use the before filter

before_filter :action, :only => ...

and authorize your others.

found this here

Upvotes: 0

Related Questions