In a LiveViewComponent: function Routes.live_path/4 is undefined (module Routes is not available)

Hi,

I’m trying to make sense of this error from a stateless LiveViewComponent, I’m trying to follow documentation for a simple live_patch helper and I’m getting the following error: function Routes.live_path/4 is undefined (module Routes is not available)

defmodule XYZWeb.MetricNavComponent do
  use Phoenix.LiveComponent

  # alias import Phoenix.LiveView.Helpers.Routes

  def render(assigns) do
    ~L"""
    <%= live_patch to: Routes.live_path(@socket, XYZWeb.MetricLive, :index, class: "some css classes")  do %>
    <span>something here</span>
    <% end %>
    """
  end
end

The entry in Router.ex:

live "/metrics", MetricLive, :index

The main LiveView load, the LiveComponent load (was loading before I start wanting to use live_patch.

Here’s what I tried:

  1. Replaced to: Routes.live_path with to: Routes.metric_path
  2. Tried to import the live helpers

From my xyx_web.ex I see the import for the live helpers, from the documentation here I seems to have the ~exact same code.

Not getting why the Routes module is not available, is it because it’s a ~L instead of a full .html.leex view?

Any help is appreciated.
Thanks

  1. You need to alias the Routes module manually since you use use Phoenix.LiveComponent directly:
alias XYZWeb.Router.Helpers, as: Routes
  1. You could replace that use with your own use XYZWeb, :live_component and now it will bring over whatever you have inside live_component in xyz_web.ex file.
1 Like

ho ok, gotcha - so it’s same as on a controller the use XYZWeb, :controller

I was wondering what it was meaning exactly, now with your reply I understand that the atom :controller or :live_component kind of executes that function from the xyz_web.ex

Coming from Go and never jumped in RoR, I’m not used to all this meta-programming and code generation, which are nice in a way, but harder when getting started, in my opinion at least.

Thank you for the head-up, highly appreciated.

I’m now getting this: function XYZWeb.Router.Helpers.live_path/4 is undefined or private

I’ve added the use XYZWeb, :live_component to the live component

I also tried to create a .html.leex instead of using the ~L thing thinking it would help, but getting the same error.

Any ideas?

Run mix phx.routes in your project directory. This mix task will list all the route helpers.

<%= live_patch to: Routes.live_path(@socket, XYZWeb.MetricLive, :index, class: "some css classes")  do %>

Here, I think you meant to pass the class option to the outer live_patch call. As is, it’s inside live_path function call.

Yes the class was not at the right place for sure.

For the function live_path, it seems I need to use the LiveView function named metric_path since using the actions.

Thanks again.