The root route tells your Rails app what page to show first when someone visits your website's main address.
Root route in Ruby on Rails
root 'controller_name#action_name'Replace controller_name with your controller's name (usually plural).
Replace action_name with the method that renders the page (like index or home).
index action in the WelcomeController.root 'welcome#index'home action in the DashboardController.root 'dashboard#home'This example sets the root route to the home action in PagesController. When you visit the main URL, it shows a plain text welcome message.
# config/routes.rb Rails.application.routes.draw do root 'pages#home' end # app/controllers/pages_controller.rb class PagesController < ApplicationController def home render plain: "Welcome to the Home Page!" end end
Only one root route should be set in your routes file.
If you don't set a root route, visiting the main URL will cause an error.
You can check your root route by running rails routes in the terminal.
The root route sets the default page for your website.
It uses the syntax root 'controller#action'.
Setting a root route helps visitors see a starting page without errors.
Practice
root route do in a Rails application?Solution
Step 1: Understand the purpose of root route
The root route defines what page loads when someone visits the main URL of the website.Step 2: Identify what root route sets
It sets the default controller action to show, so visitors see a starting page without errors.Final Answer:
It sets the default page visitors see at the website's base URL. -> Option DQuick Check:
Root route = default homepage [OK]
- Confusing root route with database or controller creation
- Thinking root route sets layout or styles
- Assuming root route is optional for homepage
home action in the pages controller?Solution
Step 1: Recall root route syntax
The standard syntax isroot 'controller#action'without extra keywords.Step 2: Check each option
root 'pages#home' matches the correct syntax exactly. root to: 'pages#home' adds unnecessaryto:, C misses quotes, D uses old hash rocket syntax incorrectly.Final Answer:
root 'pages#home' -> Option AQuick Check:
Syntax = root 'controller#action' [OK]
- Adding extra keywords like 'to:' incorrectly
- Omitting quotes around controller#action
- Using old hash rocket syntax in modern Rails
Rails.application.routes.draw do root 'welcome#index' get 'about', to: 'pages#about' end
What happens when a user visits
http://localhost:3000/?Solution
Step 1: Identify root route action
The root route is set towelcome#index, so visiting '/' triggers this action.Step 2: Understand other routes
The/aboutpath goes topages#about, but it is not the root URL.Final Answer:
The welcome#index action runs and its view is shown. -> Option CQuick Check:
Root URL = welcome#index [OK]
- Confusing root route with other get routes
- Expecting automatic redirects without code
- Assuming missing root causes errors
config/routes.rb:root 'main#home' root 'dashboard#index'
What is the problem with this code?
Solution
Step 1: Check root route rules
Rails allows only one root route. Defining multiple causes conflicts or errors.Step 2: Analyze given code
Two root routes are defined, which is invalid and will cause routing errors.Final Answer:
You cannot define more than one root route; it causes a conflict. -> Option BQuick Check:
Only one root route allowed [OK]
- Thinking last root route overrides previous silently
- Confusing root route syntax with controller naming
- Believing multiple root routes are valid
landing page from the home controller as the root route, but only if the user is not logged in. Otherwise, it should show dashboard#index. How can you set this up?Solution
Step 1: Understand root route limitation
Rails allows only one root route, so conditional logic must be inside the controller.Step 2: Implement conditional redirect
Set root to a controller action (e.g.,home#root_redirect) that checks login and redirects accordingly.Final Answer:
Set root to a controller action that redirects based on login status. -> Option AQuick Check:
Use controller logic for conditional root [OK]
- Trying to set multiple root routes with conditions
- Ignoring login status in root route setup
- Assuming routes.rb supports conditional root directly
Ready to prove it?
Pass the test and this pattern moves unknown
