Specifying the param name and nested resources

I’m trying to specify the param name of a resource so instead of just “id” I want “account_id”. I can do this by using the param option.

resources "/accounts", AccountController, param: "account_id" 

So far so good but my route is actually for nested resources. My routes looks like this:

  resources "/accounts", AccountController, param: "account_id" do
    resources "/memberships", MembershipController do
      resources "/permissions", PermissionController          
    end
    resources "/employees", EmployeeController
    resources "/departments", DepartmentController
  end

The problem I’m having is that this works for the route “/accounts/1” (conn param is “account_id”) but doesn’t work for “/accounts/1/departments” because the conn param is now “account_account_id”.

How can I set the param to “account_id” when viewing the nested resource routes too? This is because I have a plug that needs that specific param name.

2 Likes

Just an update in case anyone else has come across this issue. I have just decided to work around it by defining a resource route and scoped route with the same name as the resource so my routes now look like this:

scope "/app", App, as: :app do
  pipe_through :app
  
  resources "/accounts", AccountController, param: "account_id"

  scope "/accounts/:account_id", as: :account do  
    resources "/memberships", MembershipController do
      resources "/permissions", PermissionController          
    end
    resources "/employees", EmployeeController
    resources "/departments", DepartmentController
  end
end
5 Likes