Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails, before_filter and prepend_before_filter ordering is?

Tags:

In the following example,

before_filter :foo
before_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?

like image 266
nonopolarity Avatar asked Aug 02 '10 21:08

nonopolarity


People also ask

What is Before_filter in Ruby on Rails?

Before Filters ADVERTISEMENT. ADVERTISEMENT. Rails before filters are executed before the code in action controller is executed. The before filters are defined at the top of a controller class that calls them. To set it up, you need to call before_filter method.

What is around_ filter in Rails?

Around filters wrap an action, executing code both before and after. They may be declared as method references, blocks, or objects responding to filter or to both before and after. To use a method as an around_filter, pass a symbol naming the Ruby method. Yield (or block. call) within the method to run the action.


1 Answers

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.

like image 134
mark Avatar answered Sep 17 '22 17:09

mark