Ruby-on-rails 指定要从 before_filter 中排除的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6011764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:09:27  来源:igfitidea点击:

Specify which controllers to exclude from before_filter

ruby-on-railsdevisebefore-filter

提问by chaz hamilton

I'm using devise for authentication and have some before_filters in my application controller. Issue I'm seeing is that when I try to logout the before_filter intercepts that and keeps me on the view that's I've setup in the before_filter. Is there any way for me to specify which controllers should be excluded from the application controller or some other file?

我正在使用 devise 进行身份验证,并且在我的应用程序控制器中有一些 before_filters。我看到的问题是,当我尝试注销 before_filter 时,它会拦截它并使我保持在我在 before_filter 中设置的视图上。我有什么方法可以指定应从应用程序控制器或其他文件中排除哪些控制器?

回答by Jesse Wolgamott

In the controller where you want to skip a before filter specified in an inherited controller, you can tell rails to skip the filter

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

class ApplicationController
  before_filter :authenticate_user!
end

class SessionsController < ApplicationController
  skip_before_filter :authenticate_user!
end

回答by Don Roby

You can qualify a filter with :onlyor :except.

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

before_filter :filter_name, :except => [:action1, :action2]

Or if the filter (as I now see is the case in your situation) is defined in ApplicationControllerand you want to bypass it in a subclass controller, you can use a skip_before_filterwith the same qualifications in the subclass controller:

或者,如果过滤器(正如我现在看到的情况就是您的情况)定义在 中,ApplicationController并且您想在子类控制器中绕过它,则可以在子类控制器中使用skip_before_filter具有相同限定的a :

skip_before_filter :filter_name, :except => [:action1, :action2]

回答by Taimoor Changaiz

In config/application.rb

在 config/application.rb

config.to_prepare do
  Devise::SessionsController.skip_before_filter :authenticate_user!
end

Referenced by:

参考:

How to skip a before_filter for Devise's SessionsController?

如何跳过设计的 SessionsController 的 before_filter?

回答by akostadinov

Answers above are good except: DEPRECATION WARNING: skip_before_filter is deprecated and will be removed in Rails 5.1. Use skip_before_action instead.

上面的答案很好,除了: DEPRECATION WARNING: skip_before_filter is deprecated and will be removed in Rails 5.1. Use skip_before_action instead.

So please use before_actionand skip_before_actioninstead of *-filter.

所以请使用before_actionandskip_before_action而不是*-filter