0
0
Ruby on Railsframework~20 mins

Root route in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Root Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this root route configuration do?
Given this Rails route configuration, what will be the behavior when a user visits the root URL of the application?
Ruby on Rails
Rails.application.routes.draw do
  root 'welcome#index'
end
AIt directs the root URL to the index action of the WelcomeController.
BIt causes a syntax error because root must be inside a namespace block.
CIt redirects the root URL to the show action of the WelcomeController.
DIt sets the root URL to render a static HTML page named index.html.
Attempts:
2 left
💡 Hint
Think about what the 'root' method does in Rails routing.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this root route declaration
Which option correctly fixes the syntax error in this root route declaration?
Ruby on Rails
Rails.application.routes.draw do
  root to: 'home#main'
  root 'home#main'
end
AChange 'root to:' to 'root from:' to fix the syntax.
BReplace 'root' with 'get' to fix the syntax.
CAdd parentheses: root(to: 'home#main') to fix the syntax.
DRemove the duplicate root line; only one root route is allowed.
Attempts:
2 left
💡 Hint
Rails only allows one root route per application.
state_output
advanced
2:00remaining
What is the output when visiting root with this controller?
Given this controller and root route, what will the user see when visiting '/'?
Ruby on Rails
class HomeController < ApplicationController
  def main
    render plain: 'Welcome Home!'
  end
end

Rails.application.routes.draw do
  root 'home#main'
end
AThe browser renders the default Rails welcome page.
BThe browser shows a 404 Not Found error.
CThe browser displays the text: Welcome Home!
DThe browser displays an empty page with status 200.
Attempts:
2 left
💡 Hint
Look at the render method used in the controller action.
🔧 Debug
advanced
2:00remaining
Why does this root route cause an error?
This root route causes an error when visiting the root URL. What is the cause?
Ruby on Rails
Rails.application.routes.draw do
  root 'dashboard#show'
end
AThe root route must be defined inside a namespace block.
BThere is no DashboardController defined with a show action.
CThe root route must use double quotes, not single quotes.
DThe root route cannot point to a show action.
Attempts:
2 left
💡 Hint
Check if the controller and action exist in the app.
🧠 Conceptual
expert
2:00remaining
What happens if multiple root routes are defined?
Consider this routes file with multiple root routes. What will happen when the Rails server starts?
Ruby on Rails
Rails.application.routes.draw do
  root 'pages#home'
  root 'dashboard#index'
end
ARails raises a routing error because only one root route is allowed.
BRails uses the last root route defined and ignores the first.
CRails merges both root routes and randomly picks one at runtime.
DRails starts normally and both root routes work depending on user role.
Attempts:
2 left
💡 Hint
Think about uniqueness of the root route in Rails routing.