PeterDavidCarter

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

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

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

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

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

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

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement