Rails: before_filter

In your controller, you can have as many as you need, they get executed in the order they are set. Note how in the example below we are also storing something from the last post before the controller action runs:

class SearchesController < ApplicationController
before_filter :only => :create do |controller|
session[:last_search] = controller.params[:search][:query]

before_filter :do_something_else_method_inside_your_controller_here
before_filter :and_another_one

Also, if you need to read params, you will have to use the block version above, because params can’t be passed to methods, and won’t be readable in them if they run via before_filter

Example of using a block:

  before_filter { |c| c.valid_session_key params }

  def valid_session_key(params)