michaeljones

michaeljones

Put_session not working as expected

I’ve got the following admin-only endpoint to change the building_id for the current session:

  @spec switch_building(Plug.Conn.t(), map) :: Plug.Conn.t()
  def switch_building(conn, %{"building_id" => building_id}) do
    building = Building.get!(building_id)

    conn
    # Set the session building appropriately
    |> Plug.Conn.put_session("building_id", building.id)
    |> render("switch_building.html", target_building: building)
  end

But it doesn’t seem to be working. If I hit that page and then go to another the building_id in the session is the old one. It is a get request. I’ve also tried with |> Plug.Conn.configure_session(renew: true) before & after the put_session line. I don’t seem to understand how it is meant to work?

I’ve got a building_check “guard” plug in which I’m pulling the value out of the session again on the next request which has the following lines:

  def building_check(%Plug.Conn{assigns: %{current_user: current_user}} = conn, _opts) do
    building_id = Plug.Conn.get_session(conn, :building_id)
    ....

Can anyone help me understand what I’m doing wrong?

First Post!

dimitarvp

dimitarvp

Been a while since I last used it directly but why do you have the name of your parameter as a string and as an atom? I doubt that they are equivalent.

Most Liked

michaeljones

michaeljones

Thank you for spotting that. Unfortunately I think that session API converts everything to strings so that shouldn’t be an issue. I agree it should be consistent though!

You have helped me to re-examine my code though and realise that the route I was heading to next set the building_id again and then redirected so the changes were working but were being undone.

Thank you for your time and sorry that it was a foolish mistake on my part.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement