LiveView push_patch append to params

Here is how I solved it in my app:

  1. I store the params in handle_params as part of the assigns:

     def handle_params(params, url, socket) do
       # You could store the params as is but I like to store
       # only what has been parsed/validated.
       params = parse_params(params)
       {:noreply, assign(socket, :params, params)
     end
    
  2. Create a helper called self_path:

     def self_path(socket, action, extra) do
       Routes.live_path(socket, __MODULE__, action, Enum.into(extra, socket.assign.params))
     end
    
  3. Now I do:

     push_path(socket, to: self_path(socket, :search, %{"sort_by" => "foo"}))
17 Likes