Phoenix Flash Message

From ElixirBlocks
Revision as of 09:11, 27 December 2024 by Admin (talk | contribs) (Created page with "In Phoenix the flash message is invoked from the controller. <source> def create(conn, %{"doink" => doink_params}) do case Doinks.create_doink(doink_params) do {:ok, doink} -> conn |> put_flash(:info, "Doink created successfully.") |> redirect(to: ~p"/doinks/#{doink}") {:error, %Ecto.Changeset{} = changeset} -> render(conn, :new, changeset: changeset) end end </source> The message itself needs to be in the view...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In Phoenix the flash message is invoked from the controller.

  def create(conn, %{"doink" => doink_params}) do
    case Doinks.create_doink(doink_params) do
      {:ok, doink} ->
        conn
        |> put_flash(:info, "Doink created successfully.")
        |> redirect(to: ~p"/doinks/#{doink}")

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, :new, changeset: changeset)
    end
  end


The message itself needs to be in the view, for a new instance it defaults to being in the the layout via app_web/layouts/app.html.heex

    <.flash_group flash={@flash} />