A flash message is a way to send a one-time message from the controller to the view, usually to show notices or alerts after an action like form submission.
Click to reveal answer
beginner
How long does a flash message last in Rails?
Flash messages last for exactly one request cycle. They appear on the next page load and then disappear automatically.
Click to reveal answer
beginner
How do you set a flash message in a Rails controller?
You set a flash message by assigning a string to flash[:key], for example: flash[:notice] = "Saved successfully".
Click to reveal answer
intermediate
What is the difference between flash and flash.now in Rails?
flash sets a message for the next request, while flash.now sets a message for the current request only, useful when rendering instead of redirecting.
Click to reveal answer
beginner
Where in a Rails view do you usually display flash messages?
Flash messages are usually displayed in the application layout or a shared partial, so they appear on any page after an action.
Click to reveal answer
What does flash[:alert] typically represent in Rails?
AA permanent message
BA success message
CA debug message
DAn error or warning message
✗ Incorrect
flash[:alert] is conventionally used to show error or warning messages to the user.
Which method shows a flash message immediately without redirecting?
Aflash.now
Bflash.next
Cflash.show
Dflash.set
✗ Incorrect
flash.now sets a flash message for the current request, so it appears immediately when rendering.
Where should you place flash message display code in a Rails app?
AIn the application layout or a shared partial
BOnly in the controller
CIn the database
DIn the model
✗ Incorrect
Flash messages are shown in views, usually in the main layout or a shared partial for consistency.
What happens to a flash message after it is displayed once?
AIt repeats twice
BIt stays forever
CIt is cleared automatically
DIt moves to the database
✗ Incorrect
Flash messages are designed to last only one request cycle and then clear automatically.
How do you set a success flash message in Rails?
Aflash[:error] = "Success!"
Bflash[:notice] = "Success!"
Cflash[:alert] = "Success!"
Dflash[:fail] = "Success!"
✗ Incorrect
flash[:notice] is commonly used for success or informational messages.
Explain how flash messages work in Rails and how you would use them in a simple form submission.
Think about how you tell a user their form was saved or had errors.
You got /4 concepts.
Describe the difference between flash and flash.now and when you would use each.
Consider what happens when you redirect versus render in Rails.
You got /4 concepts.
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
Step 1: Understand flash message role
Flash messages provide feedback to users about actions they performed, like success or error notices.
Step 2: Differentiate from other features
They are temporary and do not store data permanently or affect styling or database transactions.
Final Answer:
To show temporary messages to users after actions like form submissions -> Option A
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
Step 1: Recall flash syntax in Rails
Flash messages are set using the hash-like syntax with symbols, e.g., flash[:notice] = "message".
Step 2: Identify correct syntax
Options A, B, and C use invalid Ruby or Rails syntax for flash assignment.
Final Answer:
flash[:notice] = "Saved successfully" -> Option C
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
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.
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.
Final Answer:
The alert message will be shown on the new post form page -> Option B
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
Step 1: Check render and redirect usage
In Rails, render and redirect_to are separate methods. You cannot combine them like render redirect_to.
Step 2: Identify correct usage
To redirect, use redirect_to path. To render a template, use render :template_name. Mixing both causes syntax error.
Final Answer:
Using render with redirect_to together is incorrect syntax -> Option A
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
Step 1: Understand flash persistence
By default, flash messages last for one request. flash.now shows message only for current request.
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.
Final Answer:
Use flash.keep to preserve the flash for another request -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.