Hentioe

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:

image

Can anyone tell me what is missing?

Marked As Solved

Hentioe

Hentioe

This problem does not exist because I wrote the wrong number of handle_params parameters.

Thank you all :folded_hands: :folded_hands:

Also Liked

seb3s

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 :wink:

Cheers,
Sébastien.

chrismccord

chrismccord

Creator of Phoenix

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

morgz

Useful clarification recently added by Jose when deciding whether to use mount or handle_params

Note the parameters given to c:handle_params/3 are the same as the ones given
to c:mount/3. So how do you decide which callback to use to load data?
Generally speaking, data should always be loaded on c:mount/3, since c:mount/3
is invoked once per LiveView life-cycle. Only the params you expect to be changed
via live_patch/2 or push_patch/2 must be loaded on c:handle_params/3.

https://github.com/phoenixframework/phoenix_live_view/commit/34424f37c3be388414d180536f30b208d06602bd

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement