Initializing new form with ids from the :show page of a different dependent resource


I’m struggling with my first phoenix project on managing dependent entities on different views.
I would like to create entities that depend on each other.
The example is as follows:
Site >has_many> Intervention >has_many> Task

I created each using phx.gen.html. I also included a field to select the dependence when using the :new page of each resource by loading the list of existing sites in intervention new for example.

I would like to also provide the possibility to create a new intervention from the show page of a site. Then create a new task from the show page of the intervention. I would like it to automatically set the id of the parent resources in the form.

How can I use the existing controller to do that (pass the id of site using the create controller of intervention)?
Side question: is this a case for using nested resources? If yes how deep is it reasonable to go in nesting?

Thank you!

Nested resources sounds appropriate for this if you are using the REST style. I’m not really sure what ‘too deep’ is, but 3 doesn’t seem like it yet?

1 Like

Thank you for the recommendation.

I’m still curious to know how to do it without nesting. Especially, how to set a field on the new form from the controller. Do I need to defined a dedicated method for this case?

Without nesting you can pass a param from “site show page” to “Intervention new page”
Like that /inventions/new?siteid=1. And you can read that value in eex template