views:

325

answers:

3

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

+2  A: 

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.

BaroqueBobcat
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..
Oluf Nielsen
A: 

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 %>
Joost Schuur
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.."
Oluf Nielsen
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.
Joost Schuur
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 :-)!
Oluf Nielsen
+1  A: 

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.

Shayne Sweeney