How to generate Phoenix url from Elixir map

Hi,
I have a question.

%{sort_by: :name, sort_dir: :asc}

When I have a map like the one above, I want to generate the following URL: http://localhost:4000?sort_by=name&sort_dir=asc. How can I achieve that?

index.ex

defmodule MeowWeb.MeerkatLive.Index do
...
@impl true
def handle_info({:update, opts}, socket) do
    IO.inspect(opts)
    url = # <- I want to generate url here!!!
    {:noreply, push_patch(socket, to: url, replace: true)}
end

...

router.ex

scope "/", MeowWeb do
    pipe_through :browser

    live "/", MeerkatLive.Index, :index
  end

You can create the URL like so, presuming you have access to Phoenix.VerifiedRoutes:

url = ~p"/&#{opts}"

Check the page I linked for more examples and explanations.

1 Like

Thank you it works fine!

1 Like