How to create a dynamic root in Rails 3?

routes, ruby-on-rails, ruby-on-rails-3

Solution

You can just create controller for root route.

class RoutesController < ActionController::Base
  before_filter :authenticate_user!

  def root
    root_p = case current_user.role
      when 'admin'
        SOME_ADMIN_PATH
      when 'manager'
        SOME_MANAGER_PATH
      else
        SOME_DEFAULT_PATH
      end

    redirect_to root_p
  end
end

In your routes.rb:

  root 'routes#root'

P.S. example expects using Devise, but you can customize it for your needs.

Problem

I have admins and normal users in my webapp. I want to make their root (/) different depending on who they are. The root is accessed from many different pages, so it would be much easier if I could make this happen in the routes.rb file. Here is my current file. ``` ProjectManager::Application.routes.draw do root :to => "projects#index" end ``` Can someone please link me to an example that can show me the direction to go in? Is there any way to put logic into the routes file? Thanks for all the help.

Original source