Change route with or without login in Rails
Like the title says, i would like to change the route depending on if there is a user开发者_开发百科 logged in or not, similarly to facebook. Is this possible?
You don't change the route, you just add a :before_filter
in your controller. There's a few ways to do it, but a simple example is pretend your root points at homeController#main
. On top of the controller you have something like
before_filter :check_login, :only => {:main}
And then in your ApplicationController you write a method like
def check_login
if !session[:userid].nil
redirect_to :controller => :users, :action => :profile
end
end
Which will check your session variable for a user and redirect to their profile if it exists, if not, then it will render your main method normally.
More information and examples here.
精彩评论