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
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?
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.
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.