Ruby-on-rails 从 before_action 中排除控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36878355/
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 00:24:01  来源:igfitidea点击:

Exclude a controller from before_action

ruby-on-railsruby-on-rails-4controller

提问by Matt C

I am using the before_actionfilter to call authenticatewhich is a method that will redirect users back to their home page if they aren't authorized to be on the page requested.

我正在使用before_action过滤器来调用authenticate这是一种方法,如果用户未被授权进入请求的页面,则将用户重定向回他们的主页。

I would like to exclude a page from this step, just for testing purposes.

我想从这一步中排除一个页面,仅用于测试目的。

What I have seen so far is that I can use exceptto exclude certain controller actions from being subject to the before_actionfilter so like:

到目前为止我所看到的是,我可以except用来排除某些控制器操作不受过before_action滤器的影响,例如:

before_action :authenticate, except: :demo_login

I can also exclude more than one action at a time like this:

我也可以像这样一次排除多个动作:

before_action :authenticate, except [:demo_login, :demo_show]

How can I exclude all actions in a specific controller?

如何排除特定控制器中的所有操作?

回答by Anthony E

Use skip_before_action :authenticatein the relevant controller.

使用skip_before_action :authenticate的相关负责人。

The format of this method is the same as before_actionso if you want to skip calling :authenticatefor a specific controller action, use:

此方法的格式与before_action如果您想跳过调用:authenticate特定控制器操作的格式相同,请使用:

skip_before_action :authenticate, only: [:show, :index]

skip_before_action :authenticate, only: [:show, :index]

The except:keyword can also be used.

except:关键字也可以使用。