rails过滤器

E、Filter chain skipping --- 跳过过滤器链

有时候在一个超类内指定对大多数子类,而不是全部子类有效的过滤器链会带来工作上的方便。

class ApplicationController < ActionController::Base

before_filter :authenticate

end

class WeblogController < ApplicationController# 会执行:authenticate 过滤器

end

class SignupController < ApplicationController

# 将不会执行:authenticate 过滤器

skip_before_filter :authenticate

end

F、Filter conditions --- 过滤器条件

过滤器可以被限制为只对指定动作有效。这可通过列出被排除的动作,或列出要包含的动作给过滤器来做到。有效的条件是 :only 或 :except ,两者都接受任意数量的方法引用。例如:

class Journal < ActionController::Base

# only require authentication if the current action is edit or delete

before_filter :authorize, :only => [ :edit, :delete ]

private

def authorize

# redirect to login unless authenticated

end

end

当给内联方法(proc)过滤器设置条件时,条件必须首先出现,并放置在圆括号内。

class UserPreferences < ActionController::Base

before_filter(:except => :new) { # some proc ... }

# ...

end

1、after_filter(*filters, &block) 或 append_after_filter(*filters, &block) 被传递的filters将被附加给过滤器数组,过滤器在这个控制器的动作完成后执行。

2、before_filter(*filters, &block) 或 append_before_filter(*filters, &block) 被传递的filters将被附加给过滤器数组,过滤器在这个控制器的动作完成之前执行。

3、around_filter(*filters) 或 append_around_filter(*filters) The passed filters will have their before method appended to the array of filters that’s run both before actions on this controller are performed and have their after method prepended to the after actions. 此filter对象必须对before和after两者作出响应。所以,如果你使用了append_around_filter A.new, B.new, 则调用堆栈看起来似这样:

B#before

A#before

A#after

B#after

4、prepend_after_filter(*filters, &block) 被传递的filters将被添加到过滤器链的头部。

5、prepend_around_filter(*filters) The passed filters will have their before method prepended to the array of filters that’s run both before actions on this controller are performed and have their after method appended to the after actions. The filter objects must all respond to both before and after. So if you do prepend_around_filter A.new, B.new, the callstack will look like:

A#before

B#before

B#after

A#after

6、prepend_before_filter(*filters, &block)

7、skip_after_filter(*filters) 从after过滤链中移除指定的过滤器。注意这只是跳过方法引用过滤器的工作,不是指proc。这对管理在继承体系内摘出一个需要不同体系的子类很有用。

你也可以通过使用 :only 和 :except 来控制跳过过滤器的动作。

8、skip_before_filter(*filters) 从before 过滤链中移除指定的过滤器。注意这只是跳过方法引用过滤器的工作,不是指proc。这对管理在继承体系内摘出一个需要不同体系的子类很有用。

你也可以通过使用 :only 和 :except 来控制跳过过滤器的动作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值