I am combining the code in the book, Building Table Views with Phoenix LiveView on to that of the book, Programming Phoenix LiveView", to deepen my understanding of the books. The former uses some older version of LiveView, and the latter uses the latest.
I hope some advices from those who read the two. I don’t completely understand how routes are composed and work by the Router.
From user inputs in a live_component, SortingComponent, I need to assemble a new path using Route.live_path
in the live_view module, Index, which is the parent of the live_component. Below is the error.
- ** (UndefinedFunctionError) function Routes.live_path/3 is undefined (module Routes is not available) - this error is shown when I extract a new path from user inputs onto a live component. There was a similar question about 2 years ago, link.
Below is where error arose in the Index live_view module. I changedpuch_patch
topush_navigate
.
def handle_info({:update, params}, socket) do
# call handle_params
handle_params(params, "dummy", socket)
path = Routes.live_path(socket, __MODULE__, params)
{:noreply, push_navigate(socket, to: path, replace: true)}
end
I also changed helpers: false
to true, use Phoenix.Router, helpers: true
and added alias of alias MarketWeb.Router.Helpers, as: Routes
to the Index module, but not works.
** (ArgumentError) no action MarketWeb.ProductLive.Index for MarketWeb.Router.Helpers.live_path/3. The following actions/clauses are supported:
live_path(conn_or_endpoint, MarketWeb.ProductLive, params \\ [])
live_path(conn_or_endpoint, MarketWeb.WrongLive, params \\ [])
(phoenix 1.7.2) lib/phoenix/router/helpers.ex:328: Phoenix.Router.Helpers.invalid_route_error/3
(market 0.1.0) lib/market_web/live/product_live/index.ex:85: MarketWeb.ProductLive.Index.handle_info/2
(phoenix_live_view 0.18.18) lib/phoenix_live_view/channel.ex:276: Phoenix.LiveView.Channel.handle_info/2
iex> Routes.__info__(:functions)
shows live_path/2
and live_path/3
. So there definately are routes.
Below are my phx.routes
❯ mix phx.routes
page_path GET / MarketWeb.PageController :home
live_dashboard_path GET /dev/dashboard Phoenix.LiveDashboard.PageLive :home
live_dashboard_path GET /dev/dashboard/:page Phoenix.LiveDashboard.PageLive :page
live_dashboard_path GET /dev/dashboard/:node/:page Phoenix.LiveDashboard.PageLive :page
* /dev/mailbox Plug.Swoosh.MailboxPreview []
user_registration_path GET /users/register MarketWeb.UserRegistrationLive :new
user_login_path GET /users/log_in MarketWeb.UserLoginLive :new
user_forgot_password_path GET /users/reset_password MarketWeb.UserForgotPasswordLive :new
user_reset_password_path GET /users/reset_password/:token MarketWeb.UserResetPasswordLive :edit
user_session_path POST /users/log_in MarketWeb.UserSessionController :create
user_settings_path GET /users/settings MarketWeb.UserSettingsLive :edit
user_settings_path GET /users/settings/confirm_email/:token MarketWeb.UserSettingsLive :confirm_email
live_path GET /guess MarketWeb.WrongLive MarketWeb.WrongLive
live_path GET / MarketWeb.ProductLive MarketWeb.ProductLive
product_index_path GET /products MarketWeb.ProductLive.Index :index
product_index_path GET /products/new MarketWeb.ProductLive.Index :new
product_index_path GET /products/:id/edit MarketWeb.ProductLive.Index :edit
product_show_path GET /products/:id MarketWeb.ProductLive.Show :show
product_show_path GET /products/:id/show/edit MarketWeb.ProductLive.Show :edit
user_session_path DELETE /users/log_out MarketWeb.UserSessionController :delete
user_confirmation_path GET /users/confirm/:token MarketWeb.UserConfirmationLive :edit
user_confirmation_instructions_path GET /users/confirm MarketWeb.UserConfirmationInstructionsLive :new
websocket WS /live/websocket Phoenix.LiveView.Socket
longpoll GET /live/longpoll Phoenix.LiveView.Socket
longpoll POST /live/longpoll Phoenix.LiveView.Socket
- Because
Route.live_patch
doesn’t work, I just tried to manually change the path by hand to see what happens,{:noreply, push_navigate(socket, to: "/?page=1&page_size=20&sort_by=id&sort_dir=desc", replace: true)}
in thehandle_info(:update, ...)
function of the Index live_view module. Even in this case, the expected product sorting doesn’t works. Though there is no more errors, the web page of the new address shows nothing but the welcome index page.
Thank you for reading this long & tedious question.