Phoenix Flash Message

From ElixirBlocks
Revision as of 09:12, 27 December 2024 by Admin (talk | contribs)
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 flash component 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} />