rails的before_filter,如果except子controller中的action skip_filter

如题,在http://stackoverflow.com/questions/2390178/skip-before-filter-in-rails 找到了答案。

 

问:

I have three controllers: dog , cat , and horse . These controllers all inherit from the controller animal . In the controller animal , I have a before filter that authenticates a user as such:

before_filter :authenticate

def authenticate
  authenticate_or_request_with_http_basic do |name, password|
    name == "foo" && password == "bar"
  end
end

 

In the show action of dog , I need to have open access to all users (skip the authentication).

If I were to write the authentication separately for dog , I could do something like this:

before_filter :authenticate, :except => :show

 

But since dog inherits from animal , I do not have access to the controller-specific actions. Adding :except => :show in the animal controller will not only skip authentication for the show action of dog , but also that of cat and horse . This behavior is not desired.

How can I skip the authentication only for the show action of dog while still inheriting from animal ?

 

答:

class Dog < Animal
  skip_before_filter :authenticate
end

  

See ActionController::Filters::ClassMethods for more info on filters and inheritance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值