rails里的filter

before_fil ter after_fil ter around_fil ter

设置fil ter的参数可以为一个方法symbol :method_a ,一个block 或者一个类, 该类的静态方法self.fil ter()会被调用 默认fil ter作用于当前类的所有act ion方法和子类的act ion方法, :on ly 和 :except用来在controller中包含或者排除act ion

before_fil ter 和after_fil ter默认是添加fil ter到fil ter chains最后, 如果要添加到最前使用prepend_before_fil ter() 和 prepend_after_fil ter()

before_fil ter 主要用来作验证 after_fil ter 主要用来控制内容(压缩,替换) around_fil ter 可以用来记录时间 设置参数必须为一个类, 该类有before(controller)和after(controller)两个方法 around_fil ter XXXFilter.new

around_fil ter的添加是嵌套的, around_fil ter A.new,B.new的结果如下 A.before B.before act ion..... B.after A.after 注意顺序和前面两个fil ter不一样
fil ter 在继承中的关系

子controller会执行所有父controller的fil ter,但是父不会执行子的fil ter
verification

verify指令可以看做一个专门抽出来的fil ter功能,当verify失败, 当前act ion就不会执行了 class BlogController < ApplicationController verify :on ly => :post_comment, :session => :user_id, :add_flash => { :note => "You must log in to comment"}, :redirect_to => :index #.... end 支持的参数如下启用条件 :on ly=>:name or [:name,...] :except=>name or [:name,...] 测试条件 :flash=>:key or [:key,...] flash中必须包含某些key :method=>:symbol or [:symbol,...] 请求必须是某些http 方法 :session=>:key or [:key,...] session中必须包含某些key 执行操作 :add_flash=>hash 把传入hash的值对写入flash中 :redirect_to=>params 跳转页面。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值