Goal
You’ll be able to define basic routes in Rails, and understand the specific mechanism (the routes file) that connects an incoming URL to the actual code that handles it.
Learn
Routes are defined in config/routes.rb, mapping URLs to controller actions:
Rails.application.routes.draw do get "/products", to: "products#index" get "/products/:id", to: "products#show" post "/products", to: "products#create" end
Each route specifies an HTTP verb (get, post, etc.), a URL pattern, and which controller#action should handle it. :id in the URL pattern is a dynamic segment — it captures whatever value appears there (like /products/42) and makes it available in the controller as a parameter.
Rather than writing out every individual route by hand, Rails provides a shortcut for the common pattern of a full set of CRUD (Create, Read, Update, Delete) routes for a resource:
resources :products
This single line generates all seven standard RESTful routes (index, show, new, create, edit, update, destroy) for products, following Rails’ naming conventions automatically — genuinely useful, since manually writing all seven individually would be repetitive and error-prone.
You can see exactly which routes exist in your application by running rails routes in the terminal — a genuinely useful debugging tool when a URL isn’t behaving as expected, showing you precisely which controller#action each route maps to.
Decision Task
A user visits /products/42 in a Rails app with the route get "/products/:id", to: "products#show" defined. Before reading on: what value would the show action receive for the :id parameter?
Show Answer
"42" — the dynamic :id segment captures whatever value appears in that position of the actual URL, making it available inside the controller action as a parameter (accessed via params[:id]), which the controller could then use to look up that specific product from the database.
Common Mistake
Manually writing out every individual CRUD route by hand instead of using the resources shortcut when a standard RESTful resource is genuinely what’s needed. This is repetitive, error-prone, and deviates from Rails’ own naming conventions unnecessarily — resources :products in one line correctly generates all seven standard routes following Rails’ expected patterns automatically.
Practice Questions
1. Write a route mapping GET requests to “/articles” to the articles controller’s index action.
Show Answer
get "/articles", to: "articles#index"
2. What does the resources :articles shortcut generate, compared to writing routes manually?
Show Answer
All seven standard RESTful routes (index, show, new, create, edit, update, destroy) for the articles resource, following Rails’ naming conventions automatically, without needing to write each one individually.
3. What does the :id in a route pattern like /products/:id actually do?
Show Answer
Captures whatever dynamic value appears in that position of the URL, making it available inside the controller as a parameter.
4. What terminal command shows you all the routes currently defined in a Rails application?
Show Answer
rails routes
5. True or False: a route must specify both an HTTP verb and a URL pattern.
Show Answer
True — a route definition needs both to correctly determine which requests it should actually handle.
Try It Yourself
Without looking back, write a route using the resources shortcut for a resource called “comments”.
Show Answer
resources :comments — generating all seven standard RESTful routes for comments automatically.
Quick Check
1. Where are routes defined in a Rails application?
Show Answer
config/routes.rb
2. What does a route map a URL pattern to?
Show Answer
A specific controller action.
3. What does resources :products generate?
Show Answer
All seven standard RESTful routes for the products resource.
4. What does a dynamic segment like :id in a route capture?
Show Answer
Whatever value appears in that position of the actual URL, made available as a parameter.
5. What command shows all currently defined routes?
Show Answer
rails routes