开发者

How to redirect user to login page when user is not logged in

When user is logged in we store its user id in the session.

session[:user_id] = user.id

now on all other links in our site, we want the user redirected if session[:user_id] == nil

The way I think it would be done is that it will be done in each of the methods of controller.

def show_customers
   if session[:user_id] == nil
     redirect_to (:controller => "authentication", :action => "login")
   #code related to show_customers goes here
end

but this will need to be done in every meth开发者_如何学Pythonod of every controller.

Is there a more sane Rails'y way to do this?


Use a before_filter to validate the session is valid, and if you dont want to put it in every controller, put it in your application_controller (since all controllers inherit from it). You can also easily exclude/restrict the actions its called for by overriding the method in your controllers.


Use a before_filter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜