Complete the code to set a flash message in a Rails controller.
flash[:notice] = [1]The flash message is set by assigning a string to flash[:notice]. Here, "Welcome!" is the message shown to the user.
Complete the code to redirect to the home page after setting a flash message.
flash[:alert] = "Access denied" [1] root_path
After setting a flash message, you use redirect_to to send the user to another page, here root_path.
Fix the error in the code to display a flash message in the view.
<% if flash[:[1]] %> <p><%= flash[:notice] %></p> <% end %>
flash[:alert] but displaying flash[:notice]The condition should check flash[:notice] to match the message displayed inside the paragraph.
Fill both blanks to create a flash message hash with keys and values.
flash = { [1]: "Success!", [2]: "Error occurred" }Flash keys are symbols like :notice and :alert. Strings like "notice" won't work as expected.
Fill all three blanks to set a flash message, redirect, and display it in the view.
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
Use flash[:notice] for success messages and flash.now[:alert] for errors shown on the same page. Use redirect_to to go to another page after success.