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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With