开发者

Ruby on Rails, before_filter and prepend_before_filter ordering is?

开发者 https://www.devze.com 2023-01-09 18:29 出处:网络
In the following example, before_filter :foo bef开发者_如何学编程ore_filter :bar before_filter :wah

In the following example,

before_filter :foo
bef开发者_如何学编程ore_filter :bar
before_filter :wah
prepend_before_filter :heehee
prepend_before_filter :haha

so then the execution orders will be:

haha, heehee, foo, bar, wah?   <-- note that haha is actually before heehee

And is there a reason not to list haha and heehee first in the first place but actually use prepend?


To my knowledge this is to solve class inheritance where you cannot define the order of the before_filter:

ApplicationController < ActionController::Base    
  before_filter :do_this_first    
  #....
end

SomeController < ApplicationController    
  before_filter :do_this_second
  #.... 
end

Here, neither of the methods defined will have preference unless you use a prepend_before_filter.


Looks like prepend_filter, in a queue of before_filters, it adds a filter in front of the queue. Hence, last come, first served.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号