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
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
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
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
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.
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









