0
0
Ruby on Railsframework~10 mins

Flash messages in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a flash message in a Rails controller.

Ruby on Rails
flash[:notice] = [1]
Drag options to blanks, or click blank then click option'
Arender
Bredirect_to
C:alert
D"Welcome!"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string for the message
Trying to call redirect_to instead of setting flash
Using render instead of setting flash
2fill in blank
medium

Complete the code to redirect to the home page after setting a flash message.

Ruby on Rails
flash[:alert] = "Access denied"
[1] root_path
Drag options to blanks, or click blank then click option'
Aredirect_to
Brender
Cflash.now
Dlink_to
Attempts:
3 left
💡 Hint
Common Mistakes
Using render instead of redirect_to after setting flash
Using flash.now which does not persist after redirect
Using link_to which is for views, not controllers
3fill in blank
hard

Fix the error in the code to display a flash message in the view.

Ruby on Rails
<% if flash[:[1]] %>
  <p><%= flash[:notice] %></p>
<% end %>
Drag options to blanks, or click blank then click option'
Anotice
Balert
Cmessage
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for flash[:alert] but displaying flash[:notice]
Using a key that was never set in flash
Not wrapping flash display in an if condition
4fill in blank
hard

Fill both blanks to create a flash message hash with keys and values.

Ruby on Rails
flash = { [1]: "Success!", [2]: "Error occurred" }
Drag options to blanks, or click blank then click option'
A:notice
B"notice"
C:alert
D"alert"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of symbols for flash keys
Mixing symbols and strings inconsistently
Using keys that are not standard flash keys
5fill in blank
hard

Fill all three blanks to set a flash message, redirect, and display it in the view.

Ruby on Rails
class UsersController < ApplicationController
  def create
    if @user.save
      flash[[1]] = "User created"
      [2] users_path
    else
      flash.now[[3]] = "Error saving user"
      render :new
    end
  end
end
Drag options to blanks, or click blank then click option'
A:notice
Bredirect_to
C:alert
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using flash.now for success messages that redirect
Using redirect_to after render
Mixing up :notice and :alert keys