Scope routes generated inside a live view app with the --web option failing

I’ve seen serval similar posts about this around here but I can’t find my exact problem.

I have a --live generate app which nests an Admin module in the web app with standard, non-live controllers. I use the html generator like so:

$ mix phx.gen.html --binary_id --web admin Admin.Pages Page pages example_field:string

This generates exactly what I want from a module and file structure standpoint. It creates a MyApp.Admin.Pages.Page domain module and a MyAppWeb.Admin.PageController web module written to lib/my_app_web/admin/page_controller.ex. So far so good! The generated templates use Routes.admin_page_path helpers which is also what I expect.

However…

When I update my router to look like this:

scope "/", MyAppWeb do
  pipe_through :browser

  live "/", HomeLive, :index

  scope "/admin", Admin do
    resources "/pages", PageController
  end
end

or even like this:

scope "/", MyAppWeb do
  pipe_through :browser

  live "/", HomeLive, :index
end

scope "/admin", MyAppWeb.Admin do
  pipe_through :browser

  resources "/pages", PageController
end

…Phoenix generates route helpers for page without the admin_ prefix!

I’m using Phoenix 1.5.3.

Welp, this is embarrassing. After struggling with this for an hour and deciding to post this question, I re-read the docs and turns out I originally skimmed over the part that talks, in detail, about just this case.

The answer is to use the :as option in the scope:

scope "/admin", MyAppWeb.Admin, as: :admin do
  resources "/pages", PageController
end

Thank you, Elixir Forums, for allowing me to rubber duck you <3

1 Like