📖 Scenario: You are building a simple web app using Rails. You want to understand how URLs connect to the code that runs when someone visits those URLs.
🎯 Goal: Learn how to set up routing in Rails so that specific URLs run specific controller actions.
📋 What You'll Learn
Create a routes file with a root URL
Add a route for a controller action
Create a controller with an action method
Connect the route to the controller action
💡 Why This Matters
🌍 Real World
Web apps need routing to know what code to run when users visit different URLs.
💼 Career
Understanding routing is essential for Rails developers to build functional web applications.
Progress0 / 4 steps
1
Set up the root route
In the config/routes.rb file, create a root route that points to the home#index action.
Ruby on Rails
Hint
The root route defines what happens when someone visits the main URL of your app.
2
Add a route for the about page
In config/routes.rb, add a route for the URL /about that maps to the home#about action.
Ruby on Rails
Hint
Use get '/about', to: 'home#about' to map the URL to the controller action.
3
Create the HomeController with index and about actions
Create a HomeController class in app/controllers/home_controller.rb with two methods: index and about. Both methods should be empty.
Ruby on Rails
Hint
Controllers in Rails are classes that inherit from ApplicationController. Actions are methods inside them.
4
Connect the routes to the controller actions
Ensure the config/routes.rb file connects the root URL to home#index and the /about URL to home#about. This completes the routing setup.
Ruby on Rails
Hint
Routing maps URLs to controller actions so your app knows what code to run for each URL.
Practice
(1/5)
1. Why does Rails use routing to map URLs to controller actions?
easy
A. To decide which code runs when a user visits a specific URL
B. To store user data in the database
C. To style the webpage with CSS
D. To send emails automatically
Solution
Step 1: Understand the purpose of routing in Rails
Routing connects a URL to a specific controller action so the app knows what code to run.
Step 2: Identify what happens when a user visits a URL
The app uses routing to find the right controller and action to respond with the correct page or data.
Final Answer:
To decide which code runs when a user visits a specific URL -> Option A
Quick Check:
Routing = URL to action mapping [OK]
Hint: Routing links URLs to controller actions [OK]
Common Mistakes:
Thinking routing stores data
Confusing routing with styling
Assuming routing sends emails
2. Which of the following is the correct syntax to define a route in Rails that maps GET requests for '/home' to the 'index' action of 'pages' controller?
easy
A. route get '/home' => 'pages#index'
B. get '/home' : 'pages#index'
C. get '/home' => pages#index
D. get '/home', to: 'pages#index'
Solution
Step 1: Recall Rails routing syntax for GET requests
The correct syntax uses get '/path', to: 'controller#action'.
Step 2: Match the syntax to the options
get '/home', to: 'pages#index' matches this pattern exactly, others have syntax errors or missing quotes.
Final Answer:
get '/home', to: 'pages#index' -> Option D
Quick Check:
Correct route syntax uses get '/path', to: 'controller#action' [OK]
Hint: Use get '/url', to: 'controller#action' format [OK]
Common Mistakes:
Missing quotes around controller#action
Using => instead of to:
Omitting commas between arguments
3. Given the route get '/profile', to: 'users#show' and the UsersController with a show action that sets @user = User.find(1), what happens when a user visits '/profile'?
medium
A. The app redirects to the homepage
B. The app runs the show action in UsersController and displays user with ID 1
C. The app runs the index action of UsersController
D. The app shows a 404 error because '/profile' is not defined
Solution
Step 1: Understand the route mapping
The route maps GET '/profile' to the show action in UsersController.
Step 2: Analyze the controller action behavior
The show action finds user with ID 1 and prepares it for the view.
Final Answer:
The app runs the show action in UsersController and displays user with ID 1 -> Option B
4. Look at this route definition: get '/dashboard' 'dashboard#index'. What is the problem here?
medium
A. Missing comma between path and controller#action
B. Wrong HTTP verb used
C. Controller name should be capitalized
D. Route path should start with a capital letter
Solution
Step 1: Check the route syntax
Rails routing requires a comma between the URL path and the controller#action string.
Step 2: Identify the missing comma
The code misses a comma after '/dashboard', causing a syntax error.
Final Answer:
Missing comma between path and controller#action -> Option A
Quick Check:
Routes need commas separating arguments [OK]
Hint: Check commas between route arguments [OK]
Common Mistakes:
Forgetting commas in route definitions
Thinking HTTP verb is wrong
Assuming capitalization matters in routes
5. You want to create a route that maps URLs like '/products/42' to the 'show' action in ProductsController, where '42' is a product ID. Which route definition correctly achieves this?
hard
A. get '/products', to: 'products#show'
B. get '/products/show/:id', to: 'products#show'
C. get '/products/:id', to: 'products#show'
D. get '/products/:product_id', to: 'products#index'
Solution
Step 1: Understand dynamic segments in routes
Rails uses ':id' in the URL to capture dynamic parts like product IDs.
Step 2: Match the route to the show action
The show action expects an ID parameter, so the route must include ':id' and map to 'products#show'.
Step 3: Evaluate options
get '/products/:id', to: 'products#show' correctly uses '/products/:id' and maps to 'products#show'. Others either add extra segments or map to wrong actions.
Final Answer:
get '/products/:id', to: 'products#show' -> Option C
Quick Check:
Dynamic segment ':id' maps URL to show action [OK]
Hint: Use ':id' for dynamic URL parts in routes [OK]
Common Mistakes:
Adding extra path segments unnecessarily
Mapping dynamic segments to wrong actions
Using wrong parameter names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.