【问题标题】:How to set a before_filter for every controller bar one?如何为每个控制器栏设置一个 before_filter?
【发布时间】:2010-09-01 13:49:29
【问题描述】:
在 Ruby on Rails 中,我想为除一个控制器之外的每个控制器添加一个 before_filter。目前我在ApplicationController:
before_filter :authenticate
有没有办法在ApplicationController 中应用此规则,而不是在每个控制器中添加before_filter :authenticate除了公共控制器?
【问题讨论】:
标签:
ruby-on-rails
authentication
before-filter
【解决方案1】:
如果您想在除一个控制器之外的每个控制器中运行此过滤器,为什么不直接跳过它?
class PublicController < ApplicationController
skip_before_filter :authenticate
end
如果你想跳过一个特定的动作,你可以使用:except:
before_filter :authenticate, :except => [ :index ]
【解决方案2】:
将前置过滤器放入ApplicationController,然后在不需要的控制器中跳过:
skip_before_filter :authenticate