Reputation: 1037
I want to override the :id
param and this explains how. But doesn't really work with nested resources.
My routes look like:
resources :users, param: :user_id do
resources :projects, param: :project_id
end
this generates url of format: :user_user_id/:project_id
. I would rather have it as :user_id/:project_id
. Can someone please help?
Upvotes: 3
Views: 2122
Reputation: 441
I know this is an old topic already answered, but as I see there is no explanation for why the code of @Зелёный works and not that of @sonalkr132 (I know this can seem off-topic, but @sonalkr132 posted another question which showed me that he didn't understand how it works)
When you create nested resources like projects
inside users
, rails you risk to have a path like users/:id/projects/:id
, which is nonsense, so rails automatically adds a prefix to the first :id
, and thus in params
you have :user_id
and :id
(referencing to :project_id
, but no doubt we are in the ProjectsController, so no further clarification is needed)
Now, when you say param: :user_id
, this adds together with the prefix user_
, that is why you get :user_user_id
- you ask rails do the job twice.
Upvotes: 5
Reputation: 44360
This code:
resources :users do
resources :projects, param: :project_id
end
Generate routes like:
user_projects GET /users/:user_id/projects(.:format) projects#index
POST /users/:user_id/projects(.:format) projects#create
new_user_project GET /users/:user_id/projects/new(.:format) projects#new
edit_user_project GET /users/:user_id/projects/:project_id/edit(.:format) projects#edit
user_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
Tested. Rails '4.2.1'
Upvotes: 0