DOKL57

DOKL57

Put flash doesn't work in LiveView

Hello! I stuck with the display flash at my LiveView template. And whatever I do, I don’t see the message on the page. I have two fields and handle_event. The content of the handle event isn’t important, but it’s important to understand that if I write an IO.input in it, I get a message in the console. But if I try to put_flash, nothing will happen

  def handle_event("click", %{"content" => params}, socket) do
          IO.inspect("test") # I see "test" in console
          put_flash(socket, :error, "Error!") # I don't see flash on page
          {:noreply, socket}
  end

I also tried to write code like this:

  def handle_event("click", %{"content" => params}, socket) do
          IO.inspect("test") # I see "test" in console
          # I don't see flash on page
          {:noreply, socket |> put_flash(socket, :error, "Error!") }
  end

and nothing happens.
Why don’t see flashes on the page? Could this be because tailwind and alphine.js were installed?

Marked As Solved

moogle19

moogle19

Normally the displaying of the flashes is handled by the css.
In a default phoenix project there is something like:

.alert:empty {
  display: none;
}

and the flash inside the live template is defined like this:

  <p class="alert alert-info" role="alert"
    phx-click="lv:clear-flash"
    phx-value-key="info"><%%= live_flash(@flash, :info) %></p>

  <p class="alert alert-danger" role="alert"
    phx-click="lv:clear-flash"
    phx-value-key="error"><%%= live_flash(@flash, :error) %></p>

Did you make changes to either of these?

Also Liked

sreyansjain

sreyansjain

LiveComponents do not render flash messages, because they are concerned only with rendering themselves.

In order to send a flash message from a live component, you can send a message to the parent live view and send the flash message from there.

moogle19

moogle19

Hi,

since Elixir variables are immutable the put_flash/3 in your first example doesn’t change your socket, but returns a new socket which you have to use.

In the second example you have to remove the socket from the put_flash. The pipe (|>) calls the next function with the return value of the previous one as first parameter automatically.

Your
socket |> put_flash(socket, :error, "Error!") is the same as
put_flash(socket, socket, :error, "Error!"), but you only want it once inside the call.

You could use either:

def handle_event("click", %{"content" => params}, socket) do
  {:noreply, put_flash(socket, :error, "Error!")}
end

or

def handle_event("click", %{"content" => params}, socket) do
  # This reassigns socket to the value returned by put_flash
  socket = 
    socket
    |> put_flash(:error, "Error!")
  {:noreply, socket}
end
PrincetonPoh

PrincetonPoh

Hi @DOKL57, sorry but I don’t understand the solution at all. It’s too complicated. After pulling my beginner hairs out, I found the solution to be super simple!!!*.

You tried 2 methods in your original code. First:

^for this, the solution is simple. Do not return {:noreply, socket}. Instead, you should return {:noreply, put_flash(socket, :error, "Error!")}. Here’s a working example:

def handle_event("save", _, socket) do
    {:noreply, put_flash(socket, :error, "Error!")}
  end

For your second approach here:

^the solution is mentioned above by moogle19. You’re piping the socket into the put_flash() which means you’re actually writing put_flash(socket, socket, :error, “Error!”). Hence the error.

From moogle19’s explanation that “Elixir variables are immutable and the put_flash returns a new socket”, I looked again at what my function was returning and so found the solution to the bug in your first approach.

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