Need a Help with Nested Resources

I am trying to add Nested Resources in router.ex like this…

resources "/phonebooks", PhonebookController do
  resources "/people", PersonController, except: [:index]

and added, templates(using phonebook_person_path() ) and controller

and then I did mix phx.routes to see, but it doesn’t compile. it says,

== Compilation error in file lib/mmarketing_web/views/dashboard/person_view.ex ==
** (CompileError) lib/mmarketing_web/templates/dashboard/person/new.html.eex:3: undefined function phonebook_person_path/2

Please tell me what I am missing :frowning:

Thank you!

What’s the output of mix phx.routes?

1 Like

I haven’t used nested resources but could it be that in your template you are not passing enough arguments to phonebook_person_path()

If you look here…

They have this example …

iex> HelloWeb.Router.Helpers.user_post_path(Endpoint, :show, 42, 17)

You could try to do something similar from iex to try to debug the problem.

I think the best were just to look at the output of mix phx.routes as we have complicated pluralisation in this example. We should take a look at what phoenix tries to do from it.

I checked on my own (just added the provided snippet in a routes file of mine) and pluralisation seems to work fine.

Then I took a closer look at the error message and have seen, that it is called with only 2 arguments, but it needs 4.

  1. the conn or endpoint
  2. the action
  3. the phonebook_id
  4. the id

Thanks! it compiles!
I i didn’t know I had to pass 2 more arguments including conn, and action.

1 Like