开发者

Rails routes root with nested resource

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

map.resources :users, :only => [:index] do |user|
  user.resources :projec开发者_如何学JAVAts
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.


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


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.


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

redirect_to user_projects_path(logged_in_user)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜