Overriding params in nested routes

梦想与她 提交于 2019-12-01 16:37:06

This is how i did it... not sure if there's a better way but it should do the trick...

resources :users, param: :user_id

resources :users, only: [] do
  resources :projects, param: :project_id
end

resources :users, only: [] do
  resources :projects, only: [] do
    resources :issues, param: :issue_id
 end
end
littlewhywhat

try member:

resources :users, param: :user_id do
  member do
    resources :projects, param: :project_id do
      member do
        resources :issues, param: :issue_id
      end
    end
  end
end

this code will generate the following paths:

      issues GET    /users/:user_id/projects/:project_id/issues(.:format)                issues#index
             POST   /users/:user_id/projects/:project_id/issues(.:format)                issues#create
   new_issue GET    /users/:user_id/projects/:project_id/issues/new(.:format)            issues#new
  edit_issue GET    /users/:user_id/projects/:project_id/issues/:issue_id/edit(.:format) issues#edit
       issue GET    /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#show
             PATCH  /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#update
             PUT    /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#update
             DELETE /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#destroy
    projects GET    /users/:user_id/projects(.:format)                                   projects#index
             POST   /users/:user_id/projects(.:format)                                   projects#create
 new_project GET    /users/:user_id/projects/new(.:format)                               projects#new
edit_project GET    /users/:user_id/projects/:project_id/edit(.:format)                  projects#edit
     project GET    /users/:user_id/projects/:project_id(.:format)                       projects#show
             PATCH  /users/:user_id/projects/:project_id(.:format)                       projects#update
             PUT    /users/:user_id/projects/:project_id(.:format)                       projects#update
             DELETE /users/:user_id/projects/:project_id(.:format)                       projects#destroy
       users GET    /users(.:format)                                                     users#index
             POST   /users(.:format)                                                     users#create
    new_user GET    /users/new(.:format)                                                 users#new
   edit_user GET    /users/:user_id/edit(.:format)                                       users#edit
        user GET    /users/:user_id(.:format)                                            users#show
             PATCH  /users/:user_id(.:format)                                            users#update
             PUT    /users/:user_id(.:format)                                            users#update
             DELETE /users/:user_id(.:format)                                            users#destroy

However, note that the helpers are not the same as in the accepted answer.

You could use 'as' option to avoid that. More agile solution with as is here: https://stackoverflow.com/a/41323489/2405850

Try these routes in routes.rb

  resources :users, :key => :user_id do
    resources :projects, :key => :project_id do
      resources :issues, :key => :issue_id
    end
  end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!