0
0
Ruby on Railsframework~30 mins

Why routing maps URLs to actions in Ruby on Rails - See It in Action

Choose your learning style9 modes available
Why routing maps URLs to actions
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Routing maps URLs to controller actions so your app knows what code to run for each URL.