before_filter语法当你想要“除”控制器“abc”

在Rails中,当你想要“除”控制器“abc”时,什么是before_filter语法。

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

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

背景 – 只需要整个应用程序的基本身份validation,除了实际处理用户身份validation的控制器….

您可以将以下行放在不应执行before_filter的控制器中:

 skip_before_filter :login_required 

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

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

这里有一个例子。


编辑:使用Rails 4, before_filter使用before_action别名, before_action也使用skip_before_action别名

before_filter语法是

 before_filter :login_required, :except => ["-name of the action-"] 

看看Rails API Doc 。

而不是使用控制器名称,我建议利用控制器从其父级inheritance其filter的事实。 所以我推荐的是这样的:

 # app/controllers/application_controller.rb class ApplicationController # no filters here end # app/controllers/authenticated_controller.rb class AuthenticatedController < ApplicationController before_filter :login_required end # app/controllers/some_other_controller.rb class SomeOtherController < AuthenticatedController # inherits the before_filter from AuthenticatedController # use this for most of your other controllers end # app/controllers/unauthenticated_controller.rb class UnauthenticatedController < ApplicationController # no filters, since this inherits directly from ApplicationController # use this for the controller that you don't want to check login on end 

这意味着控制器知道他们是否应该检查登录,而不是有一个(可能是脆弱的)名单。