0
0
Ruby on Railsframework~30 mins

Root route in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting a Root Route in Rails
📖 Scenario: You are building a simple Rails web application for a local bookstore. You want visitors to see a welcome page when they visit the main website address.
🎯 Goal: Set up a root route in Rails so that the homepage shows the home#index action when users visit the base URL.
📋 What You'll Learn
Create a controller named home with an index action
Add a root route in config/routes.rb pointing to home#index
Ensure the root route is the first route defined
Use Rails routing syntax correctly
💡 Why This Matters
🌍 Real World
Setting a root route is essential for any Rails website to define what users see first when they visit the site.
💼 Career
Understanding routing and root routes is a fundamental skill for Rails developers working on web applications.
Progress0 / 4 steps
1
Create the Home Controller with Index Action
Create a controller named home_controller.rb inside app/controllers with a class HomeController that has an empty method index.
Ruby on Rails
Need a hint?

Use class HomeController < ApplicationController and define def index inside it.

2
Open the Routes File
Open the config/routes.rb file to prepare for adding the root route.
Ruby on Rails
Need a hint?

Make sure you have Rails.application.routes.draw do and end in config/routes.rb.

3
Add the Root Route
Inside Rails.application.routes.draw do, add the root route using root 'home#index' to point the base URL to the index action of HomeController.
Ruby on Rails
Need a hint?

Use root 'home#index' inside the routes block.

4
Verify the Root Route Placement
Ensure the root route root 'home#index' is the first route inside the Rails.application.routes.draw do block in config/routes.rb.
Ruby on Rails
Need a hint?

The root route should be the first line inside the routes block.