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?

1 Like

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.

1 Like

Probably routes.jelper not imported.

1 Like

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.

1 Like

Yes, that does seem to be what’s happened, but why does this keep occurring and how do I stop it?

1 Like

As far as I remember that module gets created when compiling the router as well. Please do a mix clean and a mix build and show full transcript of the latter.

1 Like
Compiling 37 files (.ex)
warning: redefining module HaaksploitsWeb.Gettext (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.Gettext.beam)
  lib/haaksploits_web/gettext.ex:1

warning: redefining module HaaksploitsWeb.NavbarView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.NavbarView.beam)
  lib/haaksploits_web/views/navbar_view.ex:1

warning: redefining module HaaksploitsWeb.HaakView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.HaakView.beam)
  lib/haaksploits_web/views/haak_view.ex:1

warning: redefining module HaaksploitsWeb.Endpoint (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.Endpoint.beam)
  lib/haaksploits_web/endpoint.ex:1

warning: redefining module HaaksploitsWeb.UnclassifiedController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.UnclassifiedController.beam)
  lib/haaksploits_web/controllers/unclassified_controller.ex:1

warning: redefining module HaaksploitsWeb.LayoutView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.LayoutView.beam)
  lib/haaksploits_web/views/layout_view.ex:1

warning: redefining module HaaksploitsWeb (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.beam)
  lib/haaksploits_web.ex:1

warning: redefining module HaaksploitsWeb.Router (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.Router.beam)
  lib/haaksploits_web/router.ex:1

warning: redefining module HaaksploitsWeb.UserView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.UserView.beam)
  lib/haaksploits_web/views/user_view.ex:1

warning: redefining module Haaksploits.Repo (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.Haaksploits.Repo.beam)
  lib/haaksploits/repo.ex:1

warning: redefining module HaaksploitsWeb.NavbarController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.NavbarController.beam)
  lib/haaksploits_web/controllers/navbar_controller.ex:1

warning: redefining module HaaksploitsWeb.HaaksploitsController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.HaaksploitsController.beam)
  lib/haaksploits_web/controllers/haaksploits_controller.ex:1

warning: redefining module HaaksploitsWeb.UserSocket (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.UserSocket.beam)
  lib/haaksploits_web/channels/user_socket.ex:1

warning: redefining module HaaksploitsWeb.UnclassifiedView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.UnclassifiedView.beam)
  lib/haaksploits_web/views/unclassified_view.ex:1

warning: redefining module HaaksploitsWeb.SecurityController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.SecurityController.beam)
  lib/haaksploits_web/controllers/security_controller.ex:1

warning: redefining module HaaksploitsWeb.UserController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.UserController.beam)
  lib/haaksploits_web/controllers/user_controller.ex:1

warning: redefining module HaaksploitsWeb.TutorialsController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.TutorialsController.beam)
  lib/haaksploits_web/controllers/tutorials_controller.ex:1

warning: redefining module HaaksploitsWeb.PageController (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.PageController.beam)
  lib/haaksploits_web/controllers/page_controller.ex:1

warning: redefining module HaaksploitsWeb.PageView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.PageView.beam)
  lib/haaksploits_web/views/intro_view.ex:1

warning: redefining module HaaksploitsWeb.ErrorHelpers (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.ErrorHelpers.beam)
  lib/haaksploits_web/views/error_helpers.ex:1

warning: redefining module Haaksploits (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.Haaksploits.beam)
  lib/haaksploits.ex:1

warning: redefining module HaaksploitsWeb.SecurityView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.SecurityView.beam)
  lib/haaksploits_web/views/security_view.ex:1

warning: redefining module HaaksploitsWeb.ErrorView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.ErrorView.beam)
  lib/haaksploits_web/views/error_view.ex:1

warning: redefining module HaaksploitsWeb.Router.Helpers (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.Router.Helpers.beam)
  lib/haaksploits_web/router.ex:1

warning: redefining module HaaksploitsWeb.HaaksploitsView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.HaaksploitsView.beam)
  lib/haaksploits_web/views/haaksploits.ex:1

warning: redefining module HaaksploitsWeb.TutorialsView (current version loaded from /Users/user/.mix/archives/haaksploits-0.1.3/haaksploits-0.1.3/ebin/Elixir.HaaksploitsWeb.TutorialsView.beam)
  lib/haaksploits_web/views/tutorials_view.ex:1


== 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
1 Like

Please reinstall the mix archive of your project it causes module clashes.

Then try again.

1 Like

Running mix do archive.build, archive.install results in the same error.

1 Like

I meant remove, sorry for the confusion.

1 Like

I’d try doing an rm -rf _build

1 Like

Oh, I tried pushing everything to my remote repo and recloning but the problem reoccurs. I guess that’s what was meant by archive because I tried mix archive.remove but I don’t think that a thing? I also tried removing the _build directory but that doesn’t work either. I’m going to try recloning the repo in a new user-area to see if I can isolate whether it’s the code or a corruption of some sort in the environment but any further suggestions welcome.

1 Like

Easiest thing to remove an archive is to remove the corresponding subfolder from ~/.mix/archives.

A good estimate of the exact path can be made from the module redefinition warnings you posted earlier.

Also you usually do not want to install full phoenix applications as an archive. Those are meant for small extensions to mix. Not fully fledged applications.

1 Like

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.

2 Likes

I’ve removed the archive. What’s the correct way to install for a fully fledged app i.e. how should I install it now to make sure I don’t have similar issues now or in the future? I simply followed the instructions in the Phoenix Framework setup guides and was not aware of the term archive specifically.

1 Like

You usually do releases using destillery or similar tools.

Can you also please show where the guides suggest to do mix archive.install except for phx_new? That we can file a bug and correct the guides.

1 Like

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.

2 Likes

Sounds like you should submit a PR to fix the documentation on it. :slight_smile:

It is primarily just for mix plugins. :slight_smile:

And you really should learn Distillery.

This implies that there are bugs in your application as it stands. Specifically what are the things that broke?