35

Heyya guys. So i thought about this coolio idea, if you are logged in then you get some sort of dashboard, else you get an information/login/sign up page.. So how do i do that..

I mostly wants to do this in Routes = not something like


def index
  if current_user.present?
    render :action => 'logged_in'
  else
    render :action => 'logged_out'
  end
end

thanks in advance!

/ Oluf Nielsen

5 Answers 5

80

Think you may have been looking for this:

authenticated :user do
  root :to => "dashboard#show"
end

root :to => "devise/sessions#new"

Note: it's authenticate*d*

7
  • 1
    This did not work for me, as I wanted the not-logged in route to point to something else as sessions#new. Shayne's answer however works in those cases and was the solution for me...clarifying: if you set the seccond root :to entry to anything else than devise/sessions#new it will still redirect an unauthenticated user to sessions#new.
    – effkay
    Feb 6, 2011 at 17:37
  • 5
    I know I'm late, but if you use authenticated :user (notice the "d") it should work as expected Jan 17, 2012 at 22:50
  • 1
    From what I have gotten, you don't need the last line. authenticate will redirect you if you're not logged in.
    – RocketR
    Feb 16, 2012 at 12:56
  • 1
    Super useful! I didn't find this in the Devise docs, and it's an excellent, clean solution.
    – Emily
    Feb 4, 2013 at 19:42
  • 27
    For Rails 4.0 you have to make sure you have unique names for the path helpers, like root to: "dashboard#show", as: :authenticated_root. Otherwise the authenticated root and the normal root route end up having the same name for their path helpers, which Rails 4.0 no longer allows. May 10, 2013 at 9:26
25

I too wanted this in my app, here's what I came up with.

MyCoolioApp::Application.routes.draw do
  root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
  root :to => 'welcome#index'

  get "/" => 'users#dashboard', :as => "user_root"

  # ..
end

In Rails 3 you can use Request Based Contraints to dynamically map your root route. The solution above works for the Devise authentication gem but can be modified to support your own implementation.

With the above root_path or / will route to a WelcomeController#index action for un-authenticated requests. When a user is logged in the same root_path will route to UsersController#dashboard.

Hope this helps.

3
  • This is exactly what I was looking for. How would I write routing tests for this using RSpec, though? Doesn't look like authenticate? is available in the test environment? Would appreciate any guidance... thanks.
    – neezer
    Mar 14, 2011 at 23:16
  • I had to add a scope to get it working: request.env["warden"].authenticate?(:scope => :user)
    – hb922
    Sep 14, 2011 at 22:37
  • also, the new devise has a cool new method called authenticated, which does this for you
    – hb922
    Sep 14, 2011 at 22:38
21

I have the same problem and I solved it with this:

authenticated :user do
  root :to => "wathever#index"
end
unauthenticated :user do
  devise_scope :user do 
    get "/" => "devise/sessions#new"
  end
end

Hope it helps.

3
  • 2
    This worked, but instead of get "/" => ... I did root to: "devise/sessions#new" and it worked, probably the same thing - thank you! Mar 15, 2013 at 4:38
  • 1
    Could you also explain what "authenticated" and "unauthenticated" do? Oct 21, 2013 at 13:57
  • The first will match when an authenticated user exists, while the second will match when he not to exist.
    – zezim
    Nov 13, 2013 at 13:45
4

are you using devise's before filters?

class FooController < ActionController::Base
  before_filter :authenticate_user!
...

Why don't you try altering the default login views so they have the info/login/signup infos you want.

1
  • That's not what i want to.. i want to make two >> root :to => "welcome#index" << - or something like that, when you're not logged in, and something like >> root :to => "dashboard#index"<< - When you are logged in.. Sep 25, 2010 at 19:34
0

Here's what I'm using in my application layout file right now. Haven't broken it out into partials yet:

            <% if user_signed_in? %>
                <a href="/profile"><%= current_user.email %></a> | 
                <%= link_to "Logout", destroy_user_session_path %>
            <% else %>
                <%= link_to "Login", new_user_session_path %> |
                <%= link_to "Register", new_user_registration_path %>
            <% end %>
3
  • as i said up in the other one, "That's not what i want to.. i want to make two >> root :to => "welcome#index" << - or something like that, when you're not logged in, and something like >> root :to => "dashboard#index"<< - When you are logged in.." Sep 25, 2010 at 19:37
  • In that case, write a before_filter in your default root method (the one pointing to welcome#index) that checks if the user is logged in and if so, redirect_to dashboard#index. It doesn't even need to be a before_filter, you can do it straight in the index method. Sep 26, 2010 at 3:23
  • well, that's what BaroqueBobcat wrote to, so if theres no else way to make it i accepting he's solution. Thanx for the help :-)! Sep 26, 2010 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.