How to skip a before_filter for Devise's SessionsController?

前端 未结 7 1727
失恋的感觉
失恋的感觉 2020-12-01 12:46

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

How can I skip_before_filter

相关标签:
7条回答
  • 2020-12-01 12:56

    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
    
    0 讨论(0)
  • 2020-12-01 12:57

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

      if params[:controller] != 'devise/sessions'
    
    0 讨论(0)
  • 2020-12-01 13:00

    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

    0 讨论(0)
  • 2020-12-01 13:06

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

    skip_before_filter :check_subdomain!, if: :devise_controller?
    
    0 讨论(0)
  • 2020-12-01 13:13

    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.

    0 讨论(0)
  • 2020-12-01 13:19

    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
    
    0 讨论(0)
提交回复
热议问题