PeterDavidCarter
Phx.gen.json API route undefined after added to router.ex
I’ve run phx.gen.json to create an api to search and retrieve json formatted page content dynamically. After I run the command I see this message:
Add the resource to your :api scope in lib/haaksploits_web/router.ex:
resources "/pagecontent", PageContentController, except: [:new, :edit]
Remember to update your repository by running migrations:
$ mix ecto.migrate
However, after adding to the :api scope:
scope "/", HaaksploitsWeb do
pipe_through :browser
pipe_through :api
resources "/pagecontent", PageContentController, except: [:new, :edit]
I get the error:
== Compilation error in file lib/haaksploits_web/controllers/page_content_controller.ex ==
** (CompileError) lib/haaksploits_web/controllers/page_content_controller.ex:18: undefined function page_content_path/3
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
After running the ecto.migrate. I also tried like this:
scope :api do
resources "/pagecontent", PageContentController, except: [:new, :edit]
But that didn’t work either.
What’s the correct way to add the json resource to the api scope?
Marked As Solved
NobbZ
That won’t help against module clashes with a globally installed archive. That’s why I said it has to be removed first to it sorted step by step.
Also Liked
PeterDavidCarter
I tried Distillery but a bunch of things broke so I moved more low-tech. Nothing told me to do mix archive.install except I did a Google search on Phoenix Framework and ‘archive’ and that was all of relevance that came up. You seemed to be assuming I would know what you meant about removing an archive and that seemed the only way you would have. At the moment I am between jobs and my main focus is to make the app work and make money, or at least use it as a portfolio. I haven’t had time to look into all the details and I’m kind of “funds limited”. I just follow the tutorials where they seem to suit and assume if I do I’m in safe hands.
I have never done a mix archive.install except where I Google searched what you said and that seemed the only logical conclusion. The guides aren’t even clear on what the archive is, and I certainly didn’t run any commands relative to it prior to the problem, though I’d like to help get it cleared up for future users.
kokolegorille
I think it should be
scope "/api", HaaksploitsWeb do
pipe_through :api
...
end
scope "/", HaaksploitsWeb do
pipe_through :browser # Use the default browser stack
...
end
You might also show page_content_controller.ex because the error is inside.
NobbZ
Probably routes.jelper not imported.
PeterDavidCarter
Hhhhhmmm. I changed to /api but it’s the same.
page_content.controller.ex:
defmodule HaaksploitsWeb.PageContentController do
use HaaksploitsWeb, :controller
alias Haaksploits.PageData
alias Haaksploits.PageData.PageContent
action_fallback HaaksploitsWeb.FallbackController
def index(conn, _params) do
pagecontent = PageData.list_pagecontent()
render(conn, "index.json", pagecontent: pagecontent)
end
def create(conn, %{"page_content" => page_content_params}) do
with {:ok, %PageContent{} = page_content} <- PageData.create_page_content(page_content_params) do
conn
|> put_status(:created)
|> put_resp_header("location", page_content_path(conn, :show, page_content))
|> render("show.json", page_content: page_content)
end
end
def show(conn, %{"id" => id}) do
page_content = PageData.get_page_content!(id)
render(conn, "show.json", page_content: page_content)
end
def update(conn, %{"id" => id, "page_content" => page_content_params}) do
page_content = PageData.get_page_content!(id)
with {:ok, %PageContent{} = page_content} <- PageData.update_page_content(page_content, page_content_params) do
render(conn, "show.json", page_content: page_content)
end
end
def delete(conn, %{"id" => id}) do
page_content = PageData.get_page_content!(id)
with {:ok, %PageContent{}} <- PageData.delete_page_content(page_content) do
send_resp(conn, :no_content, "")
end
end
end
It was autogenned by the phx client, but I am seeing this error in my IDE next to the HaaksploitsWeb, :controller line:
(CompileError) module HaaksploitsWeb.Router.Helpers is not loaded and could not be found
I’ve seen this error a few times when doing various things with my app and it often seems to go away when I reclone the repo, but not this time.
PeterDavidCarter
Yes, that does seem to be what’s happened, but why does this keep occurring and how do I stop it?







