views:

54

answers:

3

I have a nested resource in my routes.rb like this:

map.resources :users, :only => [:index] do |user|
  user.resources :projects
end

which gives me URLs like /users/2/projects, which will show all projects owned by user 2. After a user is signed in, I'd like this to be the root page, using map.root. How would I set map.root to enable this? I'm using devise, so I can get the current user with current_user, but I'm not sure this is available in routes.rb.

+2  A: 

We're solving this with a HomepageController that renders two different templates based on if current_user.

Tass
Thanks for this. I was expecting routes.rb to deal with things like this, but I suppose it makes sense to keep that kind of logic out of there. Sorted!
Skilldrick
+1  A: 

You'd set up your route to a RootController controller in routes.rb alongside your existing nested route:

map.root :controller => :root

The controller RootController's index action could then render the index action of the ProjectsController:

class RootController < ApplicationController
  def index
    render :controller => :projects, :action => :index
  end
end

And, finally, ProjectsController would make use of current_user to render the appropriate list of projects:

class ProjectsController < ApplicationController
  def index
    @projects = Project.all.find_by_user(current_user)
  end
end

This glosses over details of authentication etc.

Richard Cook
A: 

You could redirect to that page after authentication in your filter method:

redirect_to user_projects_path(logged_in_user)
Ed Haywood