ruby-on-railsroutesactiondispatch

Overriding params in nested routes


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.


Solution

  • 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