如何跳过Devise的SessionsController的before_filter?

时间:2021-10-20 15:27:35

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

我的ApplicationController中有一个before_filter;也就是说,对于我项目中的每个控制器。

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

我怎样才能为Devise的SessionsController创建动作skip_before_filter?

7 个解决方案

#1


16  

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.

或者你可以修补Devise的会话控制器。

#2


63  

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

#3


18  

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

我最近在application_controller中遇到过滤器这个问题我用skip_before_filter解决了它

skip_before_filter :check_subdomain!, if: :devise_controller?

#4


2  

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

这是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 / environments / development.rb中添加类似这样的内容:

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

#5


1  

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

#6


1  

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

您只需检查过滤器方法是否是设计控制器。

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

#7


0  

new answer

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

如何将before_filter包装在一个除非由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

在这里找到了

#1


16  

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.

或者你可以修补Devise的会话控制器。

#2


63  

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

#3


18  

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

我最近在application_controller中遇到过滤器这个问题我用skip_before_filter解决了它

skip_before_filter :check_subdomain!, if: :devise_controller?

#4


2  

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

这是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 / environments / development.rb中添加类似这样的内容:

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

#5


1  

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

#6


1  

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

您只需检查过滤器方法是否是设计控制器。

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

#7


0  

new answer

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

如何将before_filter包装在一个除非由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

在这里找到了