1

I want to set the root depending on the cookie/session. If the user is loged in already I want to check on the cookie/session and render a different page accordingly as the root url.

I want to do something like this

if (cookie with userID) do
   root 'user/index'
else
   root 'welcome/index'
end

Is there a way I could access the ApplicationController or something similar so that I could check on everything?

Thanks

Constantin Jacob
  • 488
  • 1
  • 8
  • 21

1 Answers1

3

I think you should use a different approach here. For example, you can map root path always to same action and put all logic into that action:

class UsersController < ApplicationController
  def index
    redirect_to welcome_path and return unless logged_in?
    ... # rest of your code here 
  end
end

NOTE: Assuming logged_in? is a method that loads current user from session or returns nil.

Probably it's a good idea to move this kind of logic into a before_filter (in Rails4 before_action):

class UsersController < ApplicationController
  before_filter :require_login

  def index
  end

  private

  def require_login
    redirect_to welcome_path unless logged_in?
  end
end

If most of your application (controllers scope) depends on a logged user, move it to the ApplicationController (filter will run on each request). You can skip it using skip_before_filter :require_login in the particular cases without this requirement.

By the way, if you want to achieve it via your routes, you can play with constraints (doc here). Example code:

YourApp::Application.routes.draw do
  root 'users#index', constraints: lambda { |request| request.session['user_id'].present? }
  root 'welcome#index'
end

More ideas in this question: How can I redirect a user's home (root) path based on their role using Devise?

Community
  • 1
  • 1
markets
  • 6,709
  • 1
  • 38
  • 48
  • Would it be a good idea to put it directly into the ApplicationController so that it is called every time ? – Constantin Jacob May 05 '14 at 19:42
  • If most of your application (controllers) depends on a logged user, yes it is! Then you can skip it with `skip_before_filter :require_login` for the particular cases. – markets May 05 '14 at 19:51
  • You're welcome @ConstantinJacob. I just updated answer with more ideas. Remember, accept and upvote (if you think it appropriate) answers is useful for the community :) – markets May 05 '14 at 20:22
  • 1
    I will. I'm still working on making the first solution work...kinda hard. But I don't want to use any gem. I want to do it myself! – Constantin Jacob May 05 '14 at 20:29
  • I'm doing something wrong. I can still load the a in theory restricted website :/ – Constantin Jacob May 05 '14 at 20:47
  • What is `logged_in?`supposed to be? I have a private method in the ApplicationController `@current_user ||= User.find.(session[:user_id]) if session[:user_id]` – Constantin Jacob May 05 '14 at 20:49
  • Yes, it looks good. Typo here (try to remove `.`): `User.find(session[:user_id])`? – markets May 05 '14 at 20:54
  • No. Doesn't work. I set the session userid to nil in a logout action and I also deleted the localhost cookie myself in my browser. It still renders a controller where I haven't added the `skip_before_filter`. So in theory it should redirect me to the root url? – Constantin Jacob May 05 '14 at 21:01
  • Yes. Make sure you're destroying properly the session (you can use `reset_session` method). Btw, it's difficult without seeing the code man. – markets May 05 '14 at 21:46
  • We could continue the conversation there. I just added some comments to the gist. – markets May 05 '14 at 22:44