PeterDavidCarter

PeterDavidCarter

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?

Showing Posts 1 to 10

kokolegorille

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

NobbZ

Probably routes.jelper not imported.

PeterDavidCarter

PeterDavidCarter OP

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

PeterDavidCarter OP

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

NobbZ

NobbZ

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.

PeterDavidCarter

PeterDavidCarter OP

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
NobbZ

NobbZ

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

Then try again.

PeterDavidCarter

PeterDavidCarter OP

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

NobbZ

NobbZ

I meant remove, sorry for the confusion.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’d try doing an rm -rf _build

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews