开发者

before_filter with another controller

开发者 https://www.devze.com 2023-02-03 19:01 出处:网络
I am trying to create an action th开发者_StackOverflow社区at will check for every page if the user is logged in.

I am trying to create an action th开发者_StackOverflow社区at 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消