blackfist

blackfist

Having trouble with the Phoenix Getting Started guide - router.ex getting double path prefix to controller

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

Marked As Solved

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.

Also Liked

blackfist

blackfist

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?

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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement