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:
Replaced to: Routes.live_path with to: Routes.metric_path
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?
You need to alias the Routes module manually since you use use Phoenix.LiveComponent directly:
alias XYZWeb.Router.Helpers, as: Routes
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.
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.