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 method 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.
使用before_filter 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.