Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 w/ Devise: How to set two separate homepages based on whether the user is authenticated or not?

I am using Rails 3 and Devise to create an app where users arrive to the website and are shown a homepage containing a login and a signup form. This page has its own controller ("homepage") so it's route is

root :to => "homepage#index"

I want to display a different homepage if the users are already logged in. This would account to having the root point to

root :to => "dashboard#index"

Is there a way to have a conditional route in routes.rb, that would allow me to check whether the user is authenticated before routing them to one of those homepages?

I tried using the following code but if I'm not logged in, devise asks me to log in, so clearly only the first route works.

authenticate :user do
  root :to => "dashboard#index"
end
  root :to => "homepage#index"

Also, I want the url to point to www.example.com in both cases, so that www.example.com/dashboard/index and www.example.com/homepage/index never appear in the browser.

Thanks a million !!!

like image 515
Andrei Polmolea Avatar asked Jan 17 '12 00:01

Andrei Polmolea


1 Answers

Try this, it's specific to Warden/Devise though.

root to: "dashboard#index", constraints: lambda { |r| r.env["warden"].authenticate? }
root to: "homepage#index"
like image 52
Bradley Priest Avatar answered Sep 24 '22 18:09

Bradley Priest