Overriding params in nested routes

给你一囗甜甜゛ 提交于 2019-12-01 15:58:47

问题


I want to have consistency in naming convention of my routes. Default param is :id for any resource. However if one nests another resource in it, param of parent resource changes to :parent_id. My routing looks like:

resources :users do
  resources :projects do
    resources :issues
  end
end

For user model it would generate url like :id, for project url will be :user_id/:id and for issues url will be :user_id/:project_id/:id. I tried to overload the default param following this

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

It generates url like: :user_user_id/:project_project_id/:issue_id. Since I am overriding it I would want that that default appending of parent resource is off. I want my routes to look like :user_id/:project_id/:issue_id and I want it to be consistent for all the models. ie project should be mapped to :user_id/:project_id.


回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/32284593/overriding-params-in-nested-routes

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