Lesson 5 — Toast
Toast notifications are ephemeral messages that appear, persist briefly,
and disappear — without interrupting the user’s workflow. They differ
from Alert in that they float over the UI, queue automatically, and
auto-dismiss.
This also replaces the flash message pattern introduced in Module 6. Flash messages are functional but have three problems: they tell you the obvious, they stick around until you navigate away, and they push the layout out of shape. Toasts solve all three.
Architecture
Toast requires two parts:
- A container — fixed-position, always present in
AppLayout - A toast — an individual notification with auto-dismiss
Components::ToastContainer
|
|
The aria-live="polite" attribute makes the container a live region —
screen readers announce new toasts as they appear without interrupting
the current reading flow.
Add to AppLayout just before the closing body tag:
|
|
Components::Toast
|
|
The Stimulus controller
|
|
Triggering toasts from the server
The cleanest way to show a toast after a server action is via a Turbo Stream that appends to the container:
|
|
In a controller action:
|
|
The toast appears without a page reload. Include ToastHelper in
ApplicationController:
|
|