2008年5月7日 星期三

filter

define the actions to do before(after) some actions

two define syntaxes:
1. use method to define filter
2. use class to define filter

before_filter:
define the actions to do before some actions
ex:
before_filter :check
do check before any action. 
If check return false,  the action is not done

after_filter:
define the actions to do after some actions

filter & inheritance :
B extends A, then A's filter is done first, then B's filter is done

filter's scope:
only for some actions:
ex:
before_filter :check,  :only=>:index
before_filter :check,  :only=>[:index,  :new]

not for some actions:
before_filter :check,  :except=>:index

around_filter:
the combination of before_filter & after_filter
ex:
class TestFilter
    def  before(controller)
         @name="peter"
    end

    def  after(controller)
         @name ="andy"
    end
end

around filter  TestFilter.new

沒有留言: