RoR Flash Messages
Flash messages are short notifications that appear once and then disappear. They tell users what just happened — "Your profile was updated," "You have been logged out," or "Invalid email or password." Rails has a built-in flash system that stores these messages for exactly one request and then clears them automatically.
How Flash Works
User submits form
|
v
Controller saves the record
|
v
Controller sets flash: flash[:notice] = "Post created!"
|
v
Controller redirects to /posts/1
|
v
Browser follows the redirect → GET /posts/1
|
v
View reads flash[:notice] → displays "Post created!"
Flash is cleared after this request
|
v
User refreshes the page → flash is gone
Setting Flash Messages in Controllers
def create
@post = Post.new(post_params)
if @post.save
flash[:notice] = "Post created successfully."
redirect_to @post
else
flash[:alert] = "Could not create post. Please fix the errors below."
render :new, status: :unprocessable_entity
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:notice] = "Post has been deleted."
redirect_to posts_path
end
Shorthand for redirect_to
Rails lets you set the flash inline with the redirect:
redirect_to @post, notice: "Post created successfully." redirect_to root_path, alert: "You do not have permission to do that."
This shorthand only works with :notice and :alert. For custom keys, set the flash separately.
Displaying Flash in the Layout
Place flash display code in your application layout so it appears on every page automatically:
app/views/layouts/application.html.erb
<body>
<% if flash[:notice] %>
<div class="flash-notice">
<%= flash[:notice] %>
</div>
<% end %>
<% if flash[:alert] %>
<div class="flash-alert">
<%= flash[:alert] %>
</div>
<% end %>
<main>
<%= yield %>
</main>
</body>
Display All Flash Types Dynamically
If you use multiple flash key types, loop through all of them:
<% flash.each do |type, message| %>
<div class="flash flash-<%= type %>">
<%= message %>
</div>
<% end %>
This renders any flash key — notice, alert, success, error, info, or any custom key you define.
Standard Flash Key Conventions
| Key | Typical Use | Visual Style |
|---|---|---|
:notice | Success messages | Green |
:alert | Warnings and errors | Red or orange |
:success | Positive confirmation | Green |
:error | System or permission errors | Red |
:info | Neutral information | Blue |
flash.now — Flash for the Current Request
Regular flash survives until the next request (after a redirect). When you render a view directly (no redirect), use flash.now so the message shows immediately and does not carry over to the next request.
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Post created." ← regular flash (after redirect)
else
flash.now[:alert] = "Please fix the errors below." ← flash.now (render only)
render :new, status: :unprocessable_entity
end
end
When to use which: redirect_to + flash → message shows on the next page (after redirect) render + flash.now → message shows on the current rendered page
Custom Flash Keys
You can use any key you want:
flash[:promo] = "Use code SAVE20 for 20% off your first order!"
flash[:warning] = "Your account expires in 3 days. Renew now."
flash[:confirmation] = "A verification email has been sent to #{@user.email}."
Display them in the layout using the flash.each loop shown above.
Keeping Flash for One More Request
In rare cases you need a flash message to survive an extra request:
flash.keep(:notice) ← keeps only the notice for one more request flash.keep ← keeps all flash messages for one more request
Use this sparingly. It is usually a sign you need to redesign the flow.
Flash in the Real World
Action Taken Flash Message ---------------------------------------------------- User signs up "Welcome! Your account is ready." User logs in with wrong password "Invalid email or password." User updates their profile "Profile updated successfully." User deletes a record "Item has been deleted." User tries a restricted page "You must be logged in to do that." Password reset email sent "Check your inbox for reset instructions."
Flash messages give users immediate, clear feedback. They bridge the gap between actions and their results, making your application feel responsive and trustworthy.
