Bird
Raised Fist0
Ruby on Railsframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of flash messages in a Rails application?
easy
A. To show temporary messages to users after actions like form submissions
B. To permanently store user data in the session
C. To style the webpage with CSS dynamically
D. To handle database transactions automatically

Solution

  1. Step 1: Understand flash message role

    Flash messages provide feedback to users about actions they performed, like success or error notices.
  2. Step 2: Differentiate from other features

    They are temporary and do not store data permanently or affect styling or database transactions.
  3. Final Answer:

    To show temporary messages to users after actions like form submissions -> Option A
  4. Quick Check:

    Flash messages = temporary user feedback [OK]
Hint: Flash messages show quick feedback after user actions [OK]
Common Mistakes:
  • Thinking flash stores data permanently
  • Confusing flash with CSS or styling
  • Assuming flash manages database
2. Which of the following is the correct way to set a flash notice in a Rails controller?
easy
A. flash.notice = "Saved successfully"
B. flash->notice = "Saved successfully"
C. flash[:notice] = "Saved successfully"
D. flash('notice') = "Saved successfully"

Solution

  1. Step 1: Recall flash syntax in Rails

    Flash messages are set using the hash-like syntax with symbols, e.g., flash[:notice] = "message".
  2. Step 2: Identify correct syntax

    Options A, B, and C use invalid Ruby or Rails syntax for flash assignment.
  3. Final Answer:

    flash[:notice] = "Saved successfully" -> Option C
  4. Quick Check:

    flash[:key] = message [OK]
Hint: Use flash[:notice] = "message" to set flash [OK]
Common Mistakes:
  • Using dot notation instead of brackets
  • Using parentheses incorrectly
  • Using arrow syntax which is invalid
3. Given this controller code snippet:
def create
  if @post.save
    flash[:notice] = "Post created!"
    redirect_to posts_path
  else
    flash[:alert] = "Error creating post"
    render :new
  end
end

What will happen to the flash messages if the post fails to save?
medium
A. The notice message will be shown on the posts index page
B. The alert message will be shown on the new post form page
C. No flash message will be shown because render does not keep flash
D. Both notice and alert messages will be shown on the new page

Solution

  1. Step 1: Understand flash behavior with redirect and render

    Flash messages persist for one request. Redirect clears flash after showing it. Render keeps the current request, so flash remains.
  2. Step 2: Analyze code flow on failure

    On failure, flash[:alert] is set and render :new is called, so alert message shows on the new form page.
  3. Final Answer:

    The alert message will be shown on the new post form page -> Option B
  4. Quick Check:

    flash with render = message shown immediately [OK]
Hint: Flash with render shows message immediately; with redirect, next page [OK]
Common Mistakes:
  • Assuming flash clears on render
  • Thinking notice shows on failure
  • Confusing redirect and render flash behavior
4. Identify the error in this Rails controller code snippet:
def update
  if @user.update(user_params)
    flash[:notice] = "Profile updated"
    render redirect_to profile_path
  else
    flash[:alert] = "Update failed"
    redirect_to edit_profile_path
  end
end
medium
A. Using render with redirect_to together is incorrect syntax
B. flash[:notice] should be flash.notice
C. redirect_to cannot be used inside an if statement
D. flash messages must be set after redirect_to

Solution

  1. Step 1: Check render and redirect usage

    In Rails, render and redirect_to are separate methods. You cannot combine them like render redirect_to.
  2. Step 2: Identify correct usage

    To redirect, use redirect_to path. To render a template, use render :template_name. Mixing both causes syntax error.
  3. Final Answer:

    Using render with redirect_to together is incorrect syntax -> Option A
  4. Quick Check:

    render and redirect_to are separate calls [OK]
Hint: Use either render or redirect_to, never both together [OK]
Common Mistakes:
  • Combining render and redirect_to
  • Using dot notation for flash
  • Setting flash after redirect instead of before
5. You want to show a flash message that persists for the current and the next request (for example, after a redirect and then another page load). Which method should you use in Rails?
hard
A. Use flash.now to set the message
B. Use flash.delete to save the message
C. Use flash.clear to keep the message
D. Use flash.keep to preserve the flash for another request

Solution

  1. Step 1: Understand flash persistence

    By default, flash messages last for one request. flash.now shows message only for current request.
  2. Step 2: Use flash.keep to extend flash life

    flash.keep preserves the flash message for an additional request, so it appears again after redirect.
  3. Final Answer:

    Use flash.keep to preserve the flash for another request -> Option D
  4. Quick Check:

    flash.keep extends flash life beyond one request [OK]
Hint: Use flash.keep to keep flash message for next request too [OK]
Common Mistakes:
  • Using flash.now which lasts only current request
  • Confusing flash.clear with preserving messages
  • Using flash.delete which removes messages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard