rails过滤器

来源:互联网 发布:电脑必备软件 编辑:程序博客网 时间:2024/05/17 01:07
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 来控制跳过过滤器的动作。

原创粉丝点击