DOKL57

DOKL57

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?

Showing Posts 1 to 10

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
DOKL57

DOKL57 OP

Thanks for your answer! I changed code to

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

but still got nothing. I’m inclined to believe that the display error is due to the tailwind css setup.

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?

DOKL57

DOKL57 OP

Nope. My live.html.heex here:

<main class="container">
  <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>

  <%= @inner_content %>
</main>

And app.css contains:

}
.alert:empty {
  display: none;
}
moogle19

moogle19

Hmm, strange.

Do you have :fetch_live_flash in the pipeline in your router?

Sebb

Sebb

you have to use a layout in your liveview like

use Phoenix.LiveView, layout: {FooWeb.LayoutView, "live.html"}

see: Live layouts — Phoenix LiveView v1.2.5

scoop

scoop

As a follow up to a question I asked earlier on the forum regarding implementing functions to be invoked from the templates.

In this case, one would perhaps use Phoenix.LiveView.JS to compose several JS commands to extend the generated flashes. I’m thinking of Chris McChord’s demo for the release of LiveView 0.17.

Where would such code reside?

chrisdel101

chrisdel101

EDIT: SOLVED I tried this and it WORKED, i.e. calling inside handle_event. Delgating to another function send caused it to fail. I wasn’t sending the socket back to handle_event properly (I guess)

def handle_event("alert", values, socket) do
    {:noreply, put_flash(socket, :error, "TEST ERROR")}
  end

I have this problem too. I’ve tried all the suggestions above.

Notes:

  • default alert html is from live.html (not app.html.heex) as the class=XXXXXXXXXXX is on the page. This happens even if I remove the layout definition use Phoenix.LiveView, layout...

  • non-live views have the app.html.heex flash layout as expected

  • The flash message does not display, and the message text is not injected

  • The flash is appended to the socket as expected. So seems problem is that socket data is not making the html changes to the page.

This is a rough example of my code setup.

index.ex

defmodule MyApp.UserLive.Index do
  
  use MyApp, :live_view
  use Phoenix.LiveView, layout: {MyApp.LayoutView, "live.html"}

    def handle_events(call, _values, socket) do
       send(socket)
    end
    
    def send(socket) do
     socket =
        socket
        |> put_flash(:info, "Alert sent successfully.")
        {:noreply, socket}
    end
end

live.html (layout called above)

<main class="container XXXXXXXXXXX">
  <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>

  <%= @inner_content %>
</main>

router

import Phoenix.LiveView.Router
pipeline :browser do
  ...
    plug :fetch_live_flash
    plug :put_root_layout, {TurnStileWeb.LayoutView, :root}
   ...
  end
scope "/", MyApp do
    pipe_through :browser
    live "/myRoute",
         UserLive.Index,
         :index
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.

manicar2093

manicar2093

Hi!. I don’t know if this can help. I did have the same problem but in a LiveComponent Form. I put_flash a message but it wasn’t displayed. I noticed LiveComponent does not use <.flash_group flash={@flash} /> in layout but if you add this in your LiveComponent will work

defmodule AppTestWeb.PostLive.FormComponent do
use CharlyTeamCoachAppWeb, :live_component

  @impl true
  def render(assigns) do
    ~H"""
    <div>
      <.flash_group flash={@flash} />
      <.simple_form
        for={@form}
        id="post-form"
      />
          <!-- inputs here -->
     </.simple_form>
end

Its kind of weird. I thougth layout component would be used :confused:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews