博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rails过滤器
阅读量:4042 次
发布时间:2019-05-24

本文共 2273 字,大约阅读时间需要 7 分钟。

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 来控制跳过过滤器的动作。

转载地址:http://jdadi.baihongyu.com/

你可能感兴趣的文章
1022. Digital Library (30)(字符串分割) 模拟
查看>>
1024. Palindromic Number (25) 回文字符串
查看>>
1051. Pop Sequence (25) 判断出栈序列是否合理
查看>>
判断出栈序列是否合理
查看>>
1026. Table Tennis (30)
查看>>
1028. List Sorting (25) COUT和 cin 超时
查看>>
1064. Complete Binary Search Tree (30)
查看>>
1098. Insertion or Heap Sort (25)
查看>>
1099. Build A Binary Search Tree (30) 给定二叉搜索树插值
查看>>
1083. List Grades (25)
查看>>
1036. Boys vs Girls (25)
查看>>
1094. The Largest Generation (25)
查看>>
1056. Mice and Rice (25)
查看>>
1030. Travel Plan (30)寻找最短路径
查看>>
1053. Path of Equal Weight (30)
查看>>
1073. Scientific Notation (20)
查看>>
1037. Magic Coupon (25)
查看>>
1040. Longest Symmetric String (25) 最长回文子串
查看>>
1090. Highest Price in Supply Chain (25)
查看>>
1063. Set Similarity (25) 并查集
查看>>