Search code examples
ruby-on-railsrubybefore-filter

before_filter with another controller


I am trying to create an action that will check for every page if the user is logged in. For that, in the controller Home I created this method:

def check_session
  if !session[:user_id]
    redirect_to :action=> 'login'
  end
end

And I've put this code at the head of the controller:

before_filter :check_session, :except => [:sub_layout, :authenticate, :login]

Now I want to use check_session from outside the pages of Home, lets say in the pages of Users. What is the correct syntax for calling a method of a different controller in the before_filter?


Solution

  • If you move everything you already have to the application_controller it will check for every controller in your app. Then use the :skip_before_filter method to bypass the check for whatever controllers/actions you want.