Overriding params in nested routes
Asked Answered
I

3

10

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.

Inkhorn answered 29/8, 2015 at 9:30 Comment(0)
R
12

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
Rancidity answered 19/9, 2015 at 4:33 Comment(0)
B
10

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://mcmap.net/q/1162174/-override-nested-named-route-parameters

Blackfish answered 25/12, 2016 at 18:40 Comment(1)
you could additionally wrap the resource in scope(as: :project) to correct url_helpers. This solution is superior to the accepted as it allows changing the param key to anything, even if it does not include the parent resource nameOvertrade
W
-1

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
Whoredom answered 29/8, 2015 at 11:39 Comment(5)
Sorry Haider, but it only adds a key to parameters lists: Parameters: {"key"=>:project_id, "user_id"=>"addie", "id"=>"new_project"}Inkhorn
@Inkhorn you want to convert ` /customers/:customer_id/projects/:project_id/issues` to ` /:user_user_id/:project_project_id/:issue_id` ????Whoredom
No! I want it other way round. ie :user_user_id/:project_project_id/:issue_id to :user_id/:project_id/:issue_id. Overriding the param generates the former .Inkhorn
@Inkhorn currently what you have define in routes.rb will produce routes something like /users/:user_id/projects/:project_id/issues/:id not like what you are saying :user_user_id/:project_project_id/:issue_idWhoredom
I know. I want it to look like users/:user_id/projects/:project_id/issues/:issue_id and I want the same name of param for all cases. ie users/:user_id/projects/:project_id and user/:user_id. I tried to achieve it by overriding the param which didn't work.Inkhorn

© 2022 - 2024 — McMap. All rights reserved.