开发者

Non-standard routes in Rails

开发者 https://www.devze.com 2022-12-31 11:12 出处:网络
I currently have a routes.rb file that looks like this: map.resources :profiles do |profile| profil开发者_Go百科e.resources :projects, :has_many => :tasks

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

map.resources :profiles do |profile|
  profil开发者_Go百科e.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.


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


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
0

精彩评论

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