Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiveView Router Helpers doc for live_path incomplete / out-of-date #3666

Closed
ericridgeway opened this issue Feb 7, 2025 · 2 comments · Fixed by #3667
Closed

LiveView Router Helpers doc for live_path incomplete / out-of-date #3666

ericridgeway opened this issue Feb 7, 2025 · 2 comments · Fixed by #3667

Comments

@ericridgeway
Copy link
Contributor

In the past, if I wanted to change the url params while staying on the same liveview page, we would do:

  path = Routes.live_path(socket, __MODULE__, params)
  {:noreply, push_patch(socket, to: path, replace: true)}

The Phoenix.LiveView.Router documentation still seems to want us to do that

But that no longer seems to work with a fresh Phoenix install on the current version:

** (UndefinedFunctionError) function Routes.live_path/3 is undefined (module Routes is not available)

The push_patch documentation also doesn't help, as it has no examples of using a helper to lookup the route for the current page/liveview module. All the examples are hardcoded, like "/" or "/my_live_view"

What is the correct way to push_patch or push_navigate to the SAME current page I'm on. How do I get that path, and is it possible to update the doc with such an example?

In particular, can we clarify or correct the live/4 documentation to better explain it's "By default, you can generate a route to this LiveView by using the live_path helper" bit? I can find no such "live_path" helper

@ericridgeway
Copy link
Contributor Author

The current workaround:

  def handle_params(_params, url, socket) do
    socket
    |> assign(path: URI.parse(url).path)
    |> noreply
  end

Then in some handle_event, for example a sorter:

  def handle_event("sort", %{"field" => field_raw}, socket) do
    field = String.to_existing_atom(field_raw)

    new_url_params = %{sort_by: field, sort_dir: :asc} |> URI.encode_query
    new_path =
      socket.assigns.path
      |> URI.parse
      |> URI.append_query(new_url_params)
      |> URI.to_string

    socket
    |> push_patch(to: new_path, replace: true)
    |> noreply
  end

Those 10 million build-the-uri-by-hand calls can't be the correct way to do this though, can they?

Is there anywhere in the doc to explain how we push_patch to the current liveview without hardcoding it's path?

@ericridgeway
Copy link
Contributor Author

Ok, more hunting found a likely explanation in Phoenix.VerifiedRoutes

So if I understand correctly, we ARE supposed to just "hardcode" the path, but using the ~p sigil

I'm pretty sure the live/4 documentation (the "live_path" part) is out of date then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant