为了清楚起见,名称和对象已被简化。基本概念保持不变。
我有三个控制器:dog
、cat
和horse
。这些控制器都继承自控制器animal
。在控制器animal
中,我有一个 before 过滤器,用于对用户进行身份验证:
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |name, password|
name == "foo" && password == "bar"
end
end
在show
操作中dog
,我需要对所有用户开放访问(跳过身份验证)。
如果我要单独为 编写身份验证dog
,我可以这样做:
before_filter :authenticate, :except => :show
但由于dog
继承自animal
,我无权访问特定于控制器的操作。添加控制器不仅会跳过对 的动作的认证:except => :show
,还会跳过和的动作。这种行为是不希望的。animal
show
dog
cat
horse
我如何才能跳过身份验证仅用于同时仍继承自的show
操作?dog
animal