blackfist

blackfist

I’m following the directions for the Getting Started project at hexdocs.pm. I’ve gotten to the guide on Contexts here: https://hexdocs.pm/phoenix/contexts.html. I’ve added a resources line to my router.ex so that my “/” scope looks like this:

scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :home
    get "/hello", HelloController, :index
    get "/hello/:messenger", HelloController, :show
    resources "/products", ProductController
  end

But instead of getting routes that point to HelloWeb.ProductController, my routes all point to HelloWeb.HelloWeb.ProductController. Even more strange, if I put in the name of some other non-existent controller I get routes that look the way I expected. Is there something about the generated ProductController that is prepending an extra HelloWeb onto the generated routes?

Any idea what I’m doing wrong here?

EDIT to add an example of what I mean. If I change that line to resources "/products", NilController then my resulting mix phx.routes looks like this:

GET     /products                              HelloWeb.NilController :index
GET     /products/:id/edit                     HelloWeb.NilController :edit
GET     /products/new                          HelloWeb.NilController :new
GET     /products/:id                          HelloWeb.NilController :show
POST    /products                              HelloWeb.NilController :create
PATCH   /products/:id                          HelloWeb.NilController :update
PUT     /products/:id                          HelloWeb.NilController :update
DELETE  /products/:id                          HelloWeb.NilController :delete

Showing Posts 1 to 4

josevalim

josevalim

Creator of Elixir

Can you post the full router.exs file? :slight_smile:

blackfist

blackfist OP

I think I found the problem @josevalim. Either my editor or the context generator or my cat added alias HelloWeb.ProductController to the top of router.ex. When I comment that line out I no longer have the problem.

defmodule HelloWeb.Router do
  alias HelloWeb.ProductController
  use HelloWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {HelloWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :home
    get "/hello", HelloController, :index
    get "/hello/:messenger", HelloController, :show
    resources "/products", ProductController
  end

  # Other scopes may use custom stacks.
  # scope "/api", HelloWeb do
  #   pipe_through :api
  # end

  # Enable LiveDashboard and Swoosh mailbox preview in development
  if Application.compile_env(:hello, :dev_routes) do
    # If you want to use the LiveDashboard in production, you should put
    # it behind authentication and allow only admins to access it.
    # If your application does not have an admins-only section yet,
    # you can use Plug.BasicAuth to set up some basic authentication
    # as long as you are also using SSL (which you should anyway).
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through :browser

      live_dashboard "/dashboard", metrics: HelloWeb.Telemetry
      forward "/mailbox", Plug.Swoosh.MailboxPreview
    end
  end
end

That wont cause any future problems for me if I comment out that second line, will it?

aesmail

aesmail

Remove the alias in line 2.

It was probably added by the editor automatically (although mistakenly) when you tried to add the resources. This issue happened to me once and I went crazy trying to figure out what happened.

kwando

kwando

I hate that feature so much, those auto-added aliases has tripped me up a few times as well. If you don’t see it happen you will start to get errors that doesn’t seem to make any sense at all.

— All posts loaded —

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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews