用户未登录时如何将用户重定向到登录页面 [英] How to redirect user to login page when user is not logged in
本文介绍了用户未登录时如何将用户重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户登录时,我们将其用户 ID 存储在会话中.
When user is logged in we store its user id in the session.
session[:user_id] = user.id
现在在我们网站的所有其他链接上,如果 session[:user_id] == nil
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 method of every controller.
是否有更合理的 Rails 方法来做到这一点?
Is there a more sane Rails'y way to do this?
推荐答案
使用 before_filter.
这篇关于用户未登录时如何将用户重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文