【发布时间】:2011-01-24 07:31:56
【问题描述】:
为清楚起见,名称和对象已被简化。基本概念保持不变。
我有三个控制器:dog、cat 和 horse。
这些控制器都继承自控制器animal。
在控制器animal 中,我有一个前置过滤器,用于对用户进行身份验证:
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |name, password|
name == "foo" && password == "bar"
end
end
在dog 的show 操作中,我需要对所有用户开放访问(跳过身份验证)。
如果我要单独为dog 编写身份验证,我可以这样做:
before_filter :authenticate, :except => :show
但由于dog 继承自animal,我无法访问特定于控制器的操作。在animal 控制器中添加:except => :show 不仅会跳过dog 的show 操作的身份验证,还会跳过cat 和horse 的身份验证。这种行为是不希望的。
我怎样才能只为dog 的show 操作跳过身份验证,同时仍然从animal 继承?
【问题讨论】:
标签: ruby-on-rails inheritance before-filter