before_filter 语法,当你想“除外"时控制器“abc" [英] before_filter syntax when you want to "except" controller "abc"

查看:52
本文介绍了before_filter 语法,当你想“除外"时控制器“abc"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 中,当您想排除"控制器abc"时,before_filter 语法是什么.

In Rails what is the before_filter syntax when you want to "except" controller "abc".

例如,在 application_controller 中如果我想说:

Example, in the application_controller if I want to say:

before_filter :login_required :except => ["-name of controller-"]

背景 - 只需要在整个应用程序中进行基本身份验证,除了实际处理用户身份验证的控制器......

Background - Just wanted basic authentication across the whole app except the controller that actually handles getting a user authenticated....

推荐答案

您可以在控制器中不应该执行 before_filter 的地方放置以下行:

You can put the following line in the controller where the before_filter should not be executed:

skip_before_filter :login_required

您甚至可以使用 :only:except 选项指定忽略 before_filter 的方法:

You can even specifiy the methods where the before_filter is ignored with :only and :except options:

skip_before_filter :login_required, :only => [:login]

示例此处.

在 Rails 4 中,before_filter 的别名为 before_actionskip_before_filter 的别名也为 skip_before_action

with Rails 4, before_filter is aliased with before_action, and skip_before_filter is also aliased with skip_before_action

这篇关于before_filter 语法,当你想“除外"时控制器“abc"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆