How to check session with a filter?

Hey all,
I have a simple session checker function in my application.rb:
def check_session
unless !session[:state].nil?
redirect_to :controller => ‘users’, :action => ‘login’
end
end

I would like to apply that function to some actions only of my
‘machines’
controller which are edit,update,create,new and destroy. I know it has
something to do with before_filter but I’m not sure how to use it.

any idea?

thanx in advance

Pat

Patrick A. wrote:

Hey all,
I have a simple session checker function in my application.rb:
def check_session
unless !session[:state].nil?
redirect_to :controller => ‘users’, :action => ‘login’
end
end

I would like to apply that function to some actions only of my
‘machines’
controller which are edit,update,create,new and destroy. I know it has
something to do with before_filter but I’m not sure how to use it.

any idea?

thanx in advance

Pat

try:

class ApplicationController < ActionController::Base

def check_session
unless !session[:state].nil?
redirect_to :controller => ‘users’, :action => ‘login’
end

end

def MachinesController < ApplicationController
before_filter :check_session, :only => [:edit, :create, :new,
:destroy]

end

If you only have those 4 methods in the controller thenyou can delete
the :only part of that filter.

–jake