zamith
I’m creating a search form with filters and want both the query and the filters to change the URL. The filters and query can be set separately or together, and handle_params can already deal with either being empty.
What I’m doing is triggering a push_patch on events (filter click, form submit, clear search). Something like this:
def handle_event("update_filter", %{"filter" => filter}, socket) do
{:noreply,
push_patch(socket,
to: Routes.live_path(socket, Index, query: socket.assigns.query, filter: filter)
)}
end
def handle_event("search", %{"query" => query}, socket) do
{:noreply,
push_patch(socket,
to: Routes.live_path(socket, Index, query: query, filter: socket.assigns.filter)
)}
end
This works, but my “problem” is that I have to set all the url params every time I call push_patch. Is there any way to say append this param to the current path? Alternatively, is there any other design pattern I should be following to set this up?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cnck1387
I set up events to do exactly what you’re doing but with a different problem, but I wonder, does it make more sense to use
live_patchlinks from your template with the query params in tact and then usehandle_paramsat the LV level to grab those params?This means the event handler would go away and having to
push_patchfrom the LV itself sincelive_patchalready pushes it to the URL.Just asking because the above applies to your example too. I wonder what folks think about this. What would be the way to do this?
What’s interesting is the
--livegenerator from Phoenix 1.5 includes making a search form using the event style but in their case, they are doing an external redirect so maybe the use case is different.zamith
The reason I’m not using
live_patchis because both filter and search are independent generic components and I don’t want to have to pass all the assigns to both. Instead, whenever they are “used”, they send the parent an event saying “Hey, someone searched for X” or “Hey, someone filtered by Y”.If I was able to use
live_patchto add to the existing URL, instead of replacing it, then I would be able to use it generically.cnck1387
Would you mind gisting the complete code for those components and how this parent event is sent?
sreyansjain
How to try it. I tried generating but no search feature got generated.
mix phx.gen.live Masters Item items item
No search thing got generated.
zamith
I’m using Surface, but you can do the same with regular LiveView.
Telaia369
I have the same problem. Desperate for a solution… https://forum.elixirforum.com/t/cleaner-push-patch-and-handle-params/35583
josevalim
Here is how I solved it in my app:
I store the
paramsinhandle_paramsas part of the assigns:Create a helper called
self_path:Now I do:
Brainiac
for whoever that comes across this thread, @josevalim’s answer will not apply if you are using actions in your router declarations, as the route helper gets renamed to match the controller, and also results in different helper arities for different helpers.
Until the day comes where where we can use
Phoenix.Controllerhelpers likecurrent_path/2to merge in params in liveview, you’ll have to update the URI manually.You will need to modify step 2 of @josevalim’s answer to something like this:
Note that:
extraarg into a string map to correctly merge into the parsed query params, which will be string keys. This is represented by a utility functionto_string_map/1which simply does a list comprehension to convert the atom key to a string.extracontains a nil value.:querykey of the newURIto nil. This new%URI{}struct needs all keys except:pathto be replaced with nil.sb8244
FWIW, you can also directly use the URI rather than calling the Routes helpers. The above code does this as well, but I wanted to provide a more concise version if you don’t need as many bells and whistles.
seva
Worth adding that instead of
URI.decode_query/1andURI.encode_query/1, you might want to use more robustPlug.Conn.Query.decode/1andPlug.Conn.Query.encode/1, which do better encoding/decoding of lists in URL (e.g.?ids[]=1&ids[]=2).I.e.