指定要从before_filter中排除的控制器

cha*_*ton 39 ruby-on-rails before-filter devise

我正在使用设计进行身份验证,并在我的应用程序控制器中有一些before_filters.问题我看到的是,当我尝试注销before_filter时会拦截那个并让我继续查看我在before_filter中设置的视图.有什么办法可以指定应该从应用程序控制器或其他文件中排除哪些控制器?

Jes*_*ott 102

在要跳过继承控制器中指定的before过滤器的控制器中,可以告诉rails跳过过滤器

class ApplicationController
  before_filter :authenticate_user!
end

class SessionsController < ApplicationController
  skip_before_filter :authenticate_user!
end
Run Code Online (Sandbox Code Playgroud)

  • 这在一个更清晰的mannor中给出了原始问题的答案,但是`skip_before_filter:filter-name,:except => [:action1,:action2]`来自接受的答案是一个很好的提示! (2认同)

Don*_*oby 78

您可以使用:only或限定过滤器:except.

before_filter :filter_name, :except => [:action1, :action2]
Run Code Online (Sandbox Code Playgroud)

或者,如果定义了过滤器(我现在看到的情况),ApplicationController并且您希望在子类控制器中绕过它,则可以在子类控制器中使用skip_before_filter具有相同资格的过滤器:

skip_before_filter :filter_name, :except => [:action1, :action2]
Run Code Online (Sandbox Code Playgroud)


Tai*_*aiz 13

在config/application.rb中

config.to_prepare do
  Devise::SessionsController.skip_before_filter :authenticate_user!
end
Run Code Online (Sandbox Code Playgroud)

参考人:

如何跳过Devise的SessionsController的before_filter?