Bird
Raised Fist0
Ruby on Railsframework~5 mins

Why routing maps URLs to actions in Ruby on Rails

Choose your learning style10 modes available
Introduction

Routing connects web addresses (URLs) to specific code that runs in your app. It helps the app know what to do when someone visits a page.

When you want to show a list of items on a webpage.
When you need to display a form for users to fill out.
When you want to handle a button click that sends data to the server.
When you want to organize your app so each URL does a clear job.
When you want to make your app easy to navigate with meaningful URLs.
Syntax
Ruby on Rails
Rails.application.routes.draw do
  get '/path', to: 'controller#action'
end
The 'get' method defines a route for a URL using the GET request.
The 'controller#action' tells Rails which code to run for that URL.
Examples
This route sends visitors of '/home' to the home action in the PagesController.
Ruby on Rails
get '/home', to: 'pages#home'
This route handles form submissions sent to '/submit' using POST, running the create action in FormsController.
Ruby on Rails
post '/submit', to: 'forms#create'
This sets the homepage URL ('/') to run the index action in WelcomeController.
Ruby on Rails
root 'welcome#index'
Sample Program

This example shows a route for '/hello' that runs the say_hello action in GreetingsController. When you visit '/hello', it displays 'Hello, world!'.

Ruby on Rails
Rails.application.routes.draw do
  get '/hello', to: 'greetings#say_hello'
end

class GreetingsController < ApplicationController
  def say_hello
    render plain: 'Hello, world!'
  end
end
OutputSuccess
Important Notes

Routes must be unique so Rails knows exactly which code to run.

Use meaningful URLs to help users and search engines understand your site.

Routes can handle different HTTP methods like GET, POST, PUT, DELETE.

Summary

Routing links URLs to specific controller actions in Rails.

This helps your app respond correctly when users visit different pages.

Defining clear routes makes your app organized and easy to use.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To decide which code runs when a user visits a specific URL -> Option A
  4. 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

  1. Step 1: Recall Rails routing syntax for GET requests

    The correct syntax uses get '/path', to: 'controller#action'.
  2. Step 2: Match the syntax to the options

    get '/home', to: 'pages#index' matches this pattern exactly, others have syntax errors or missing quotes.
  3. Final Answer:

    get '/home', to: 'pages#index' -> Option D
  4. 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

  1. Step 1: Understand the route mapping

    The route maps GET '/profile' to the show action in UsersController.
  2. Step 2: Analyze the controller action behavior

    The show action finds user with ID 1 and prepares it for the view.
  3. Final Answer:

    The app runs the show action in UsersController and displays user with ID 1 -> Option B
  4. Quick Check:

    Route maps URL to controller action output [OK]
Hint: Route URL triggers mapped controller action [OK]
Common Mistakes:
  • Assuming undefined routes work
  • Confusing show with index action
  • Thinking it redirects automatically
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

  1. Step 1: Check the route syntax

    Rails routing requires a comma between the URL path and the controller#action string.
  2. Step 2: Identify the missing comma

    The code misses a comma after '/dashboard', causing a syntax error.
  3. Final Answer:

    Missing comma between path and controller#action -> Option A
  4. 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

  1. Step 1: Understand dynamic segments in routes

    Rails uses ':id' in the URL to capture dynamic parts like product IDs.
  2. 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'.
  3. 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.
  4. Final Answer:

    get '/products/:id', to: 'products#show' -> Option C
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard