How to Change Root Route Behavior in Rails 3

These instructions have only been tested with declarative_authorization. So while it may work with other authorization gems, I haven’t tested it so can’t verify. 

Say you want your users to see your marketing site (when they try to go to yourapp.com) when they are not logged in, but when they are logged in, you want them to see something different (like the dashboard of your app - which is found by going to your home controller, and index action - ‘home#index’), how do you do this in Rails 3? 


If you put an index.html file in your public directory, that takes precedence over everything else. Rails routing doesn’t even execute when that file is included (as far as I understand it anyway). So, you do it like this: In your routes.rb file, change your root route to the following:

https://gist.github.com/869820


Rename your ‘public/index.html’ file to some other name, e.g. ‘public/marketing.html’. In your home controller, do this:

https://gist.github.com/869828

Now, when a user is not logged in, they will be redirected to your static marketing page. But when they are logged in, they will go to your dashboard at home#index. Hope that helps you.

You should follow me on Twitter here: www.twitter.com/marcgayle.

>

Comments