Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise logged in root route rails 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

like image 616
Oluf Nielsen Avatar asked Sep 24 '10 21:09

Oluf Nielsen


4 Answers

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*

like image 108
Joe Lalgee Avatar answered Nov 14 '22 23:11

Joe Lalgee


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.

like image 20
Shayne Sweeney Avatar answered Nov 14 '22 22:11

Shayne Sweeney


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.

like image 21
zezim Avatar answered Nov 14 '22 21:11

zezim


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.

like image 4
BaroqueBobcat Avatar answered Nov 14 '22 21:11

BaroqueBobcat