Hentioe
How do I get path parameters in LiveView?
My router:
live "/cities/:id/users", CityLive
I tried the handle_params function but it was never called:
def handle_params(%{"id" => id}, _uri, socket) do
# some code
end
I tried to follow the practice in the chrismccord/phoenix_live_view_example project and still failed (session is always %{}):
def mount(%{path_params: %{"id" => id}}, socket) do
# some code
end
Can see the parameters from the log:

Can anyone tell me what is missing?
Marked As Solved
Hentioe
This problem does not exist because I wrote the wrong number of handle_params parameters.
Thank you all
![]()
Also Liked
seb3s
Hello All,
I found this thread while trying to get params for a “router” liveview.
This has changed very recently (with version 0.6.0) so I put this as a reminder for future readers.
Now it is possible to get params (url ids + querystrings) directly from mount/3 which now accepts params as a first argument, ex :
def mount(%{"id" => id, "token" => token} = params, session, socket) do
....
I think this is the correct way of handling it as of today ![]()
Cheers,
Sébastien.
chrismccord
You can only receive path params via the handle_params/3 callback, and handle_params is only invoked for the root LV. If you are trying to get path params to a child, you need to retrieve them from the root and pass them down to the children via assigns in the parent
morgz
Useful clarification recently added by Jose when deciding whether to use mount or handle_params
Note the parameters given to
c:handle_params/3are the same as the ones given
toc:mount/3. So how do you decide which callback to use to load data?
Generally speaking, data should always be loaded onc:mount/3, sincec:mount/3
is invoked once per LiveView life-cycle. Only the params you expect to be changed
vialive_patch/2orpush_patch/2must be loaded onc:handle_params/3.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









