0
0
Ruby on Railsframework~20 mins

Flash messages in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does the flash behave after a redirect in Rails?
Consider a Rails controller action that sets flash[:notice] = 'Saved!' and then redirects to another action. What will the user see on the redirected page if it displays flash[:notice]?
AThe message 'Saved!' will be displayed only if the redirected page sets <code>flash.now[:notice]</code>.
BThe message 'Saved!' will be displayed repeatedly on every page until the session ends.
CThe message 'Saved!' will not be displayed on the redirected page.
DThe message 'Saved!' will be displayed once on the redirected page.
Attempts:
2 left
💡 Hint
Think about how flash messages persist only for one request after a redirect.
📝 Syntax
intermediate
2:00remaining
Which code correctly sets a flash message that appears only on the current page?
You want to show a flash message that appears immediately on the current page without redirecting. Which code snippet correctly does this in a Rails controller?
Aflash[:alert] = 'Error occurred'; render :edit
Bflash[:alert] = 'Error occurred'; redirect_to edit_path
Cflash.now[:alert] = 'Error occurred'; render :edit
Dflash.now[:alert] = 'Error occurred'; redirect_to edit_path
Attempts:
2 left
💡 Hint
Use flash.now when rendering without redirecting.
🔧 Debug
advanced
2:00remaining
Why does this flash message not appear after redirect?
Given this controller code:
def create
  flash.now[:notice] = 'Created successfully'
  redirect_to root_path
end

Why will the flash message not appear on the redirected page?
Ruby on Rails
def create
  flash.now[:notice] = 'Created successfully'
  redirect_to root_path
end
A<code>flash.now</code> messages do not persist across redirects, so the message is lost.
B<code>redirect_to</code> clears all flash messages before redirecting.
CThe flash message key should be <code>:alert</code> instead of <code>:notice</code>.
DThe message will appear only if the redirected page explicitly calls <code>flash.keep</code>.
Attempts:
2 left
💡 Hint
Remember the difference between flash and flash.now.
state_output
advanced
2:00remaining
What is the content of flash after multiple redirects?
Consider this sequence in a Rails controller:
flash[:info] = 'Step 1'
redirect_to step_two_path

# In step_two action:
flash[:info] = 'Step 2'
redirect_to step_three_path

# In step_three action:
# What is flash[:info]?
Ruby on Rails
flash[:info] = 'Step 1'
redirect_to step_two_path

# step_two action
flash[:info] = 'Step 2'
redirect_to step_three_path

# step_three action
flash[:info]
A'Step 1'
B'Step 2'
Cnil
DAn error is raised because flash is reset after each redirect
Attempts:
2 left
💡 Hint
Each redirect sets a new flash message that overwrites the previous one.
🧠 Conceptual
expert
3:00remaining
Why should flash messages not be used for sensitive data?
Flash messages in Rails are stored in the session and displayed to users. Why is it a bad idea to store sensitive information (like passwords or personal data) in flash messages?
AFlash messages are stored in cookies or session and can be exposed to users or logged, risking data leaks.
BFlash messages automatically encrypt sensitive data, so storing sensitive info is safe.
CFlash messages are deleted immediately and never reach the user, so sensitive data is safe.
DFlash messages are only visible to admins, so sensitive data is protected.
Attempts:
2 left
💡 Hint
Think about where flash data is stored and who can access it.