开发者

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 开发者_如何学JAVAfor Devise's SessionsController create action ?


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


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?


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.


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


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


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜