0
0
Ruby on Railsframework~10 mins

Flash messages in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flash messages
Controller action runs
Set flash message
Redirect or render
View reads flash message
Display message to user
Flash message cleared after display
Flash messages are set in the controller, passed to the next view, displayed once, then cleared automatically.
Execution Sample
Ruby on Rails
def create
  if @post.save
    flash[:notice] = "Post created!"
    redirect_to @post
  else
    flash.now[:alert] = "Error saving post"
    render :new
  end
end
This code sets a flash message on success or failure and either redirects or renders accordingly.
Execution Table
StepActionFlash Content BeforeFlash Content AfterView RenderedMessage Displayed
1Start create action{}{}NoneNone
2Try to save @post{}{}NoneNone
3If save success{}{"notice": "Post created!"}Redirect to showPost created!
4Flash cleared after display{"notice": "Post created!"}{}Show pageNone
5If save fails{}{"alert": "Error saving post"}Render newError saving post
6Flash.now cleared after render{"alert": "Error saving post"}{}New pageNone
💡 Flash messages are cleared after being displayed once, either after redirect or render.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6
flash{}{"notice": "Post created!"}{}{"alert": "Error saving post"}{}
Key Moments - 2 Insights
Why does flash message disappear after one page load?
Flash messages are designed to last only for the next request. After the view reads and displays them (see step 4 and 6), Rails clears them automatically.
What is the difference between flash and flash.now?
flash sets a message for the next request (usually after redirect), while flash.now sets it for the current request (usually when rendering). See steps 3 vs 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the flash content after step 3?
A{"alert": "Error saving post"}
B{}
C{"notice": "Post created!"}
DNone
💡 Hint
Check the 'Flash Content After' column at step 3.
At which step does the flash message get cleared after being displayed?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Flash cleared after display' in the 'Action' column.
If we use flash.now instead of flash in a failed save, what changes in the execution table?
AFlash message appears only in the current render, not next redirect
BFlash message persists after redirect
CFlash message never displays
DFlash message appears twice
💡 Hint
Refer to steps 5 and 6 where flash.now is used and cleared after render.
Concept Snapshot
Flash messages in Rails:
- Set in controller with flash[:key] or flash.now[:key]
- flash persists for next request (redirect)
- flash.now shows message in current render
- Messages display once then clear automatically
- Used for user notifications like success or error
Full Transcript
Flash messages in Rails help show temporary messages to users. When a controller action runs, it can set a flash message using flash[:notice] or flash[:alert]. If the action redirects, the flash message is stored and shown on the next page. After the message is displayed, Rails clears it automatically so it doesn't show again. If the action renders a view directly, flash.now can be used to show a message immediately without waiting for a redirect. This way, flash messages provide friendly feedback like 'Post created!' or 'Error saving post' that appear once and then disappear.