0
0
Ruby on Railsframework~30 mins

Flash messages in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Flash messages
📖 Scenario: You are building a simple Rails web app where users can submit a form. You want to show a message after the form is submitted to tell the user if it was successful or if there was an error.
🎯 Goal: Create a Rails controller and view that use flash messages to show success or error notifications after form submission.
📋 What You'll Learn
Create a controller action that sets a flash message
Add a flash message display section in the view
Use flash[:notice] for success messages
Use flash[:alert] for error messages
💡 Why This Matters
🌍 Real World
Flash messages are used in web apps to show quick feedback to users after actions like submitting forms or logging in.
💼 Career
Knowing how to implement flash messages is important for Rails developers to improve user experience and communicate app status.
Progress0 / 4 steps
1
Create a controller with a submit action
Create a controller called MessagesController with a submit action that does nothing yet.
Ruby on Rails
Need a hint?

Use rails generate controller Messages if you want to create the file, then add the submit method.

2
Add a flash notice message in the submit action
Inside the submit action, add a line that sets flash[:notice] to the string "Form submitted successfully!".
Ruby on Rails
Need a hint?

Use flash[:notice] = "Your message" inside the action.

3
Add flash message display in the view
In the view file for the submit action (e.g., app/views/messages/submit.html.erb), add code that shows the flash notice message if it exists. Use flash[:notice] inside a <p> tag.
Ruby on Rails
Need a hint?

Use ERB tags to check and display flash[:notice].

4
Add an error flash alert and display it
Modify the submit action to also set flash[:alert] to "There was an error submitting the form.". Then update the view to show flash[:alert] inside a <p> tag if it exists.
Ruby on Rails
Need a hint?

Set flash[:alert] in the controller and check it in the view like flash[:notice].