Help with phx path helpers

I’m new to Phoenix and Elixir, and I’d like some help, please.

I’m having trouble with the helper paths not being found. I guess I don’t completely understand how they are generated. Is there something that needs to be done to generate the helper paths?
I’m using elixir 1.11.2, Phoenix 1.5.7
A simple hello world app seems to work.

I have added some controllers that try to use the helper paths and I get an error on mix phx.routes

hive_controller.ex:17: undefined function hive_path/3

In router.ex I have
scope “/”, HiveWeb do
.
.
.
resources “/hives”, HiveWeb.HiveController,
only: [:new, :create, :show]

.
.
.
I’m clearly missing something about the relationship between controllers, routes and helper paths. Does anyone have a pointer to some things to check and/or documentation that might help?

Try removing the code that causes the error and run mix phx.routes again. Those mix commands compile your app and therefore fail as well if there’s some issue.

The relationship is tech. quite simple. MyAppWeb.Router automatically at compile time creates an additional module MyAppWeb.Router.Helpers, which holds all the helper functions of that router. my_app_web.ex does alias that module for common phoenix modules like controllers and views with alias MyAppWeb.Router.Helpers, as: Routes in the macros. With that alias you can do Routes.route_name_path(…) in those modules.

1 Like

I was missing the import MyApp.Router.Helpers in my controller. Doh!

You shouldn’t need to import it. It’s already aliased for all of your controllers, but you need to use Routes.…. Phoenix used to have imports, but they can cause problems with compile times in bigger projects.

2 Likes

Got it. I removed the import and added “Routes.” before all the help path references and it works.
Thank you @LostKobrakai

1 Like