See ActionController::Filters::ClassMethods for more info on filters and inheritance.
如题,在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
本文介绍如何在Rails应用中仅针对特定控制器的某个动作跳过Before Filters。通过继承自父控制器并覆盖Before Filters的方法,可以在不影响其他子控制器的情况下实现特定控制器动作的开放访问。
865

被折叠的 条评论
为什么被折叠?



