views:

42

answers:

2

I currently have a routes.rb file that looks like this:

map.resources :profiles do |profile|
  profile.resources :projects, :has_many => :tasks
end

And this gives me routes like this:

/profiles/:profile_id/projects/:project_id/tasks

This is close to what I want, but instead of the '/profiles/:profile_id/' section I want to just have a username in place of that so the route would look something like:

/:profile_user/projects/:project_id/tasks

How can I achieve something like this? I have looked all over and haven't found anything about how to do this, but I also might not have been searching for the right thing.

A: 

I tried some options using namespace or connect, but it did not worked..

If you really want to do these routes, I think you would have to use connect and create all routes, like this:

map.connect ':profile_user/projects/:project_id/tasks', :controller => :tasks, :action => :index, :method => :get
map.connect ':profile_user/projects/:project_id/tasks/new', :controller => :tasks, :action => :new, :method => :get
map.connect ':profile_user/projects/:project_id/tasks', :controller => :tasks, :action => :create, :method => :post
robertokl
A: 

You can use with_options method:

  map.with_options(:path_prefix => ":profile_user", :name_prefix => "profile_" ) do |profile|  
    profile.resources :projects, :has_many => :tasks
  end

And then it gives you routes like:

profile_project_tasks_path(user.username, project)
# => /:profile_user/projects/:project_id/tasks

new_profile_project_task_path(user.username, project)
# => /:profile_user/projects/:project_id/tasks/new

etc

Voyta
Awesome thats is exactly what I'm looking for.
trobrock