There are two separate issues in your code in that same line, which you can fix quite easily and help improve your LiveView mental model.
Route helpers
The ‘live_path’ function will only be generated when you set a live path with a nil action in your router, as per the live/4 docs example:
scope "/", LiveviewAppWeb do
pipe_through :browser
live "/thermostat", ThermostatLive
end
allows you to reference that route as live_path(@socket, ThermostatLive)
.
If you have a look at the named helpers defined for you from the live routes you set in router.ex
, you’ll see there is no live_path
.
iex> alias LiveViewAppWeb.Router.Helpers, as: Routes
iex> Routes.__info__(:functions)
[
live_dashboard_path: 2,
live_dashboard_path: 3,
live_dashboard_path: 4,
live_dashboard_path: 5,
live_dashboard_url: 2,
live_dashboard_url: 3,
live_dashboard_url: 4,
live_dashboard_url: 5,
path: 2,
static_integrity: 2,
static_path: 2,
static_url: 2,
url: 1,
user_index_path: 2,
user_index_path: 3,
user_index_path: 4,
user_index_url: 2,
user_index_url: 3,
user_index_url: 4,
user_show_path: 3,
user_show_path: 4,
user_show_url: 3,
user_show_url: 4
]
Since your route is defined as live "/", UsersLive.Index, :index
, your path
function should be Routes.user_index_path(socket, :index, page: page)
.
live_ vs push_, and _patch vs _redirect
The live_patch/2
and live_redirect/2
functions are for generating links in your markup templates, which your users will click to execute the corresponding LiveView changes.
The push_
variants are for you to use programatically in your controllers, which annotate the socket to perform the desired action.
Then, when you want to keep the user in the same LiveView but change something about it, like pagination in your case, you should rather use the _patch/2
functions. This will update the current socket state and “patch” the page’s path params for the user, as opposed to _redirect/2
, which is used to change to a different LiveView and therefore closes the socket connection and starts up a new one.
Putting it all together, lib/liveview_app_web/live/user_live/index.ex:56 should be:
{:noreply, push_patch(socket, to: Routes.user_index_path(socket, :index, page: page))}