Reputation: 11
I'm relatively new to Elixir and Phoenix so sorry if this is a dumb question.
When trying to figure out nested routes I am getting the following error:
function QWeb.Router.Helpers.comment_path/2 is undefined or private
or
function QWeb.Router.Helpers.post_comment_path/2 is undefined or private
Using https://hexdocs.pm/phoenix/routing.html
I'm currently assuming its an arity error caused by me not knowing what I need to edit when making nested routes, as I get a warning when starting the server flagging my Heex files within the comment folder asking me if I meant to use:
* post_comment_path/3
* post_comment_path/4
* post_comment_path/5
* post_path/2
* post_path/3
Currently my router:
resources "/posts", PostController do
resources "/comments", CommentController
end
If I run mix.phx.routes I get the following:
post_comment_path GET /posts/:post_id/comments
post_comment_path GET /posts/:post_id/comments/:id/edit
post_comment_path GET /posts/:post_id/comments/new
post_comment_path GET /posts/:post_id/comments/:id
post_comment_path POST /posts/:post_id/comments
post_comment_path PATCH /posts/:post_id/comments/:id
PUT /posts/:post_id/comments/:id
post_comment_path DELETE /posts/:post_id/comments/:id
I'm able to go to http://localhost:4000/posts and render the page fine. I can create a post and get http://localhost:4000/posts/1.
If I then go to http://localhost:4000/posts/1/comments/new I get the error above stating the comment_path is undefined or private because of the Heex template errors.
The path works if I delete the below content from the prebuilt new.html.heex file
<%= render "form.html", Map.put(assigns, :action, Routes.comment_path(@conn, :create)) %>
<span><%= link "Back", to: Routes.comment_path(@conn, :index) %></span>
I've tried changing both to post_comment_path(@conn etc as well to match what it says in the router but I just get the same error with the additional post_ prefixed on to it.
If I un-nest(?) the routes and go to http://localhost:4000/comments I obviously have no issues creating comments alone as its the same as posts with a different name. So Routes.comment_path works when its not nested.
So I think my first question is where do I even start? Is this something I need to edit in the controller, the heex document, the view or alias somewhere? All the information I can find on nested routes just seems to show you how to nest the routes and see the paths that are meant to be generated in the router. I can't find any information anywhere on how to access nested routes using Routes._path within Phoenix
Any info appreciated and sorry again if this is obvious.
Upvotes: 1
Views: 363
Reputation: 331
I don't have the exact code you are doing
I've tried changing both to post_comment_path(@conn etc as well to match what it says in the router but I just get the same error with the additional post_ prefixed on to it.
However, if you are doing the nested version, remember that
<%= render "form.html", Map.put(assigns, :action, Routes.comment_path(@conn, :create)) %>
Won't work since there is no such route. I think you know this already. If you replace for the post_ as you said, just be sure that it looks as follow
<%= render "form.html", Map.put(assigns, :action, Routes._post_comment_path(@conn, :create, POST_ID)) %>
POST_ID being the post id otherwise it will fail as not defined.
Regarding nested or non-nested, it depends on you and what API you want to expose. Both should work (assuming you fix the errors you are having).
Hope it helps :)
Upvotes: 0