ActionController のフィルタ

実装の仕方

  • メソッドシンボル
class HogeController < ApplicationController
  before_filter :foobar

  private
  def foobar
  end
end
  • ブロック
class HogeController < ApplicationController
  before_filter do |controller|
  end
end
  • クラス
class HogeFilter
  def self.filter(controller)
  end
end

class HogeController < ApplicationController
  before_filter HogeFilter
end
  • aroundフィルタオブジェクト(aroundの場合のみ)
class HogeFilter
  def before(controller)
  end
  def after(controller)
  end
end

class HogeController < ApplicationController
  around_filter HogeController.new
end

フィルタチェーン

  • before, after, around の3つのフィルタがある。
  • フィルタチェーンを抜けるには、false を返す。(nilではダメ)
  • フィルタチェーンを抜けると、あとのチェーンは実行されない。