0
0
Ruby on Railsframework~30 mins

Redirect and render in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Redirect and Render in Rails Controller
📖 Scenario: You are building a simple Rails web app that manages books. You want to practice how to control what the app shows or where it sends the user after certain actions.
🎯 Goal: Learn how to use redirect_to and render in a Rails controller to control page flow and display.
📋 What You'll Learn
Create a controller action with a variable
Add a configuration variable to decide behavior
Use redirect_to or render based on the variable
Complete the controller action with correct Rails syntax
💡 Why This Matters
🌍 Real World
Redirecting and rendering are common in web apps to control what users see after actions like form submissions or page visits.
💼 Career
Understanding redirect_to and render is essential for Rails developers to manage user navigation and display content correctly.
Progress0 / 4 steps
1
Create a controller action with a variable
In the BooksController, create an action called show_message. Inside it, create a variable called message and set it to the string 'Welcome to the Book Store'.
Ruby on Rails
Need a hint?

Define a method show_message inside the controller and assign the string to message.

2
Add a configuration variable to decide behavior
Inside the show_message action, add a variable called should_redirect and set it to true.
Ruby on Rails
Need a hint?

Just add should_redirect = true inside the action.

3
Use redirect_to or render based on the variable
Inside the show_message action, add an if statement that checks should_redirect. If it is true, use redirect_to root_path. Otherwise, use render plain: message.
Ruby on Rails
Need a hint?

Use if should_redirect then redirect_to root_path, else render plain: message.

4
Complete the controller action with correct Rails syntax
Ensure the show_message action is properly closed with end keywords for both the if and the method. The full controller should be valid Ruby code.
Ruby on Rails
Need a hint?

Make sure all end keywords are present to close the method and the class.