Bird
Raised Fist0
Ruby on Railsframework~5 mins

Root route in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the root route in a Rails application?
The root route defines the default page users see when they visit the base URL of the app, like visiting www.example.com without any extra path.
Click to reveal answer
beginner
How do you set the root route in Rails?
You set it in the config/routes.rb file using root 'controller#action'. For example, root 'home#index' makes the home controller's index action the homepage.
Click to reveal answer
beginner
What happens if you don't define a root route in a Rails app?
Visiting the base URL will cause an error because Rails won't know which page to show by default.
Click to reveal answer
beginner
Can the root route point to any controller and action?
Yes, the root route can point to any controller and action you want to be the homepage, as long as that action exists and can render a view.
Click to reveal answer
beginner
Example: What does this line do? root 'dashboard#main'
It sets the root URL to show the main action inside the dashboard controller. So visiting the base URL loads that page.
Click to reveal answer
Where do you define the root route in a Rails app?
Aapp/controllers/application_controller.rb
Bconfig/routes.rb
Cconfig/database.yml
Dapp/views/layouts/application.html.erb
What does this line do? root 'welcome#home'
ARedirects all routes to welcome#home
BCreates a new controller named welcome
CSets the homepage to the home action in welcome controller
DDeletes the welcome controller
What happens if you visit the base URL without a root route defined?
AYou get an error page
BRails shows a default homepage
CRails redirects to /index
DRails shows a blank page
Can the root route point to a non-existent action?
ANo, it must point to a view file
BYes, Rails creates the action automatically
CYes, but it shows a blank page
DNo, it must point to an existing action
Which of these is a valid root route declaration?
Aroot 'pages#about'
Broot to: 'pages#about'
Croot pages#about
Droot: 'pages#about'
Explain how to set a root route in a Rails app and why it is important.
Think about the file where routes live and what the root route controls.
You got /5 concepts.
    Describe what happens if a Rails app does not have a root route defined and a user visits the base URL.
    Consider what Rails needs to know to show a page at the main URL.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the root route do in a Rails application?
      easy
      A. It specifies the layout template for all pages.
      B. It defines a new database table for the app.
      C. It creates a new controller automatically.
      D. It sets the default page visitors see at the website's base URL.

      Solution

      1. Step 1: Understand the purpose of root route

        The root route defines what page loads when someone visits the main URL of the website.
      2. Step 2: Identify what root route sets

        It sets the default controller action to show, so visitors see a starting page without errors.
      3. Final Answer:

        It sets the default page visitors see at the website's base URL. -> Option D
      4. Quick Check:

        Root route = default homepage [OK]
      Hint: Root route sets homepage URL view [OK]
      Common Mistakes:
      • Confusing root route with database or controller creation
      • Thinking root route sets layout or styles
      • Assuming root route is optional for homepage
      2. Which of the following is the correct syntax to set the root route to the home action in the pages controller?
      easy
      A. root 'pages#home'
      B. root to: 'pages#home'
      C. root pages#home
      D. root => 'pages#home'

      Solution

      1. Step 1: Recall root route syntax

        The standard syntax is root 'controller#action' without extra keywords.
      2. Step 2: Check each option

        root 'pages#home' matches the correct syntax exactly. root to: 'pages#home' adds unnecessary to:, C misses quotes, D uses old hash rocket syntax incorrectly.
      3. Final Answer:

        root 'pages#home' -> Option A
      4. Quick Check:

        Syntax = root 'controller#action' [OK]
      Hint: Use quotes and # between controller and action [OK]
      Common Mistakes:
      • Adding extra keywords like 'to:' incorrectly
      • Omitting quotes around controller#action
      • Using old hash rocket syntax in modern Rails
      3. Given this routes file snippet:
      Rails.application.routes.draw do
        root 'welcome#index'
        get 'about', to: 'pages#about'
      end

      What happens when a user visits http://localhost:3000/?
      medium
      A. The browser redirects to /about automatically.
      B. The pages#about action runs and its view is shown.
      C. The welcome#index action runs and its view is shown.
      D. A routing error occurs because root is not set.

      Solution

      1. Step 1: Identify root route action

        The root route is set to welcome#index, so visiting '/' triggers this action.
      2. Step 2: Understand other routes

        The /about path goes to pages#about, but it is not the root URL.
      3. Final Answer:

        The welcome#index action runs and its view is shown. -> Option C
      4. Quick Check:

        Root URL = welcome#index [OK]
      Hint: Root route controls '/' URL behavior [OK]
      Common Mistakes:
      • Confusing root route with other get routes
      • Expecting automatic redirects without code
      • Assuming missing root causes errors
      4. You wrote this in config/routes.rb:
      root 'main#home'
      root 'dashboard#index'

      What is the problem with this code?
      medium
      A. The syntax is wrong; root needs a block, not a string.
      B. You cannot define more than one root route; it causes a conflict.
      C. The controller names must be capitalized.
      D. This code will work fine and set the root to the last line.

      Solution

      1. Step 1: Check root route rules

        Rails allows only one root route. Defining multiple causes conflicts or errors.
      2. Step 2: Analyze given code

        Two root routes are defined, which is invalid and will cause routing errors.
      3. Final Answer:

        You cannot define more than one root route; it causes a conflict. -> Option B
      4. Quick Check:

        Only one root route allowed [OK]
      Hint: Only one root route per app allowed [OK]
      Common Mistakes:
      • Thinking last root route overrides previous silently
      • Confusing root route syntax with controller naming
      • Believing multiple root routes are valid
      5. You want your Rails app to show the 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?
      hard
      A. Set root to a controller action that redirects based on login status.
      B. Define two root routes with conditions in routes.rb.
      C. Use root 'home#landing' and add a before_action in dashboard.
      D. Set root to dashboard#index and ignore login status.

      Solution

      1. Step 1: Understand root route limitation

        Rails allows only one root route, so conditional logic must be inside the controller.
      2. Step 2: Implement conditional redirect

        Set root to a controller action (e.g., home#root_redirect) that checks login and redirects accordingly.
      3. Final Answer:

        Set root to a controller action that redirects based on login status. -> Option A
      4. Quick Check:

        Use controller logic for conditional root [OK]
      Hint: Use controller action to redirect root conditionally [OK]
      Common Mistakes:
      • Trying to set multiple root routes with conditions
      • Ignoring login status in root route setup
      • Assuming routes.rb supports conditional root directly
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard