How to get proper routes using mix phx.gen.html when you have nested resources in router.ex?

Hi all, to keep it short, I have nested resources inside of the router page like so

    resources "/groups", GroupController   
      resources "/addresses", AddressController

When I run mix phx.gen.html I want to create a resource “shipping” that’s also nested. Unfortunately this complicates the _path function that’s used in many places.

I tried running

mix phx.gen.html Shipments Shipment shipments

but this creates a simple shipment_path in all of the files that then breaks the code and doesn’t allow me to run mix phx.routes, for example, making it hard to even see what the route would be. Is there a standard way to run the mix phx.gen.html command with nested resources?

I don’t think such functionality exists in the generators.
Also when you run the generators you are advised to create the routes in the router yourself by adding code like below yourself.

resources "/groups", GroupController

Generators are there to help, but i think nested resources need to be taken care of manually.

as a follow up I realized I can change this to groups_addresses_path because that’s the path phoenix uses with other nested resources I have. Why doesn’t it still find the function if I replace all shipment_path() to groups_shipment_path()

Hi,

I use this method, ie a top level resource with multiple nested resources, it works as you have described. Do you have any sample code?

Andrew

    resources "/groups", GroupController   
      resources "/addresses", AddressController

say you have this setup

if you run mix phx.gen.html Addresses Address addresses

in all your files you will get an

address_path(conn, :page ) function.

In order to convert it, you need to make the signature

groups_address_path(conn, :page, group_id: whats in url after 'group/' and before address)

So just replace all address_path(conn, :page) to group_address_path(conn, :page, group_id)