在Rails中跳过before_filter [英] Skip before_filter in Rails

查看:349
本文介绍了在Rails中跳过before_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了清楚起见,简化了名称和对象。基本概念保持不变。

Names and objects have been simplified for clarity's sake. The basic concept remains the same.

我有三个控制器: dog cat
这些控制器都继承自控制器 animal
在控制器 animal 中,我有一个before过滤器,用于对用户进行身份验证:

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

show dog 的操作,我需要对所有用户具有开放访问权限(跳过身份验证)。

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

如果我要为 dog 单独编写身份验证,我可以这样做:

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

before_filter :authenticate, :except => :show

但是因为 dog 继承自 animal ,我无法访问特定于控制器的操作。添加:except => :动物控制器中的show 不仅会跳过 show 操作的身份验证 dog ,还包括 cat horse 。不希望出现这种情况。

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 behaviour is not desired.

如何仅针对<$ c $的 show 操作跳过身份验证c> dog ,但仍然继承自 animal

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, :only => :show
end

参见 ActionController :: Filters :: ClassMethods ,了解有关过滤器和继承的更多信息。

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

这篇关于在Rails中跳过before_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆