First I tried to access the session variable in the routes.rb file and put a simple if statement. If the user is logged in go to entry#index else go to landing#index. However, routes.rb does not seem to have access to session variables. I have session[:user_id] set and can use it to verify login status. Is it best to set the logged out page as home in routes.rb and then redirect in that controller if the user is logged in or is there a better way that I don’t know about?
Here is what I did in the controller for the logged out user home page.
def index
if User.find_by_id(session[:user_id])
redirect_to entry_url
end
end
It works fine but not sure if there are any issues with this or a better way. Any thoughts would be appreciated.
The routes actually do have access to session variables. You can set a constraint to route logged in users to a different root like below:
It’s important to note two things: first that the order matters here; second that the constraint can be refactored into a one line route unless you plan to use the constraint further (which I always do).