ruby-on-railsroutesactiondispatch

Overriding Named Route Parameters with nested resources


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?


Solution

  • 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'