Flash messages show short, temporary messages to users after actions like saving or deleting. They help users know what happened.
Flash messages in Ruby on Rails
flash[:notice] = "Your message here" flash[:alert] = "Warning or error message" redirect_to some_path
Use flash[:notice] for normal messages and flash[:alert] for warnings or errors.
Flash messages last only for the next request, usually after a redirect.
flash[:notice] = "Profile updated successfully"
redirect_to profile_pathflash[:alert] = "You must log in first"
redirect_to login_pathflash[:notice] = "Item added to cart"This controller action tries to save a new item. If it works, it sets a success flash message and redirects to the item's page. If it fails, it sets an error flash message and shows the form again.
class ItemsController < ApplicationController def create @item = Item.new(item_params) if @item.save flash[:notice] = "Item was successfully created." redirect_to @item else flash.now[:alert] = "Failed to create item." render :new end end private def item_params params.require(:item).permit(:name, :price) end end
Flash messages are stored in the session and cleared after being shown once.
Use flash.now if you want the message to appear on the current page without redirecting.
Flash messages give quick feedback to users after actions.
They last only for the next page load, usually after redirects.
Use flash[:notice] for normal info and flash[:alert] for warnings or errors.
Practice
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 AQuick Check:
Flash messages = temporary user feedback [OK]
- Thinking flash stores data permanently
- Confusing flash with CSS or styling
- Assuming flash manages database
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 CQuick Check:
flash[:key] = message [OK]
- Using dot notation instead of brackets
- Using parentheses incorrectly
- Using arrow syntax which is invalid
def create
if @post.save
flash[:notice] = "Post created!"
redirect_to posts_path
else
flash[:alert] = "Error creating post"
render :new
end
endWhat will happen to the flash messages if the post fails to save?
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 BQuick Check:
flash with render = message shown immediately [OK]
- Assuming flash clears on render
- Thinking notice shows on failure
- Confusing redirect and render flash behavior
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
endSolution
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 AQuick Check:
render and redirect_to are separate calls [OK]
- Combining render and redirect_to
- Using dot notation for flash
- Setting flash after redirect instead of before
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 DQuick Check:
flash.keep extends flash life beyond one request [OK]
- Using flash.now which lasts only current request
- Confusing flash.clear with preserving messages
- Using flash.delete which removes messages
Ready to prove it?
Pass the test and this pattern moves unknown
