Rails routes: basics
This course covers how a Rails app decides which URLs it answers. It starts with config/routes.rb and the draw block that holds every route. It then shows routes written by hand with an HTTP verb, a URL pattern and a controller action, including dynamic segments. Later lessons cover resources and its only: and param: options, named routes with _path and _url helpers, the single root route, and reading the full route list with bin/rails routes. A final lesson traces one route through every idea.
What you'll learn
- ✓How config/routes.rb and its draw block map requests to controller actions
- ✓Writing routes with get, post, patch and delete, plus dynamic segments
- ✓Generating RESTful routes with resources, narrowed by only: and param:
- ✓Named routes and the difference between _path and _url helpers
- ✓Declaring the root route and why only one is allowed
- ✓Listing and filtering routes with bin/rails routes and grep
config/routes.rb: the entry point map
In Rails console (IRB), app.get "/articles/1" only worked because a matching entry already existed somewhere. That somewhere is config/routes.rb, the file that decides which URLs an app answers and which controller action handles each.
Read the full lesson free →Curriculum
7 lessonsconfig/routes.rb: the entry point map
Shows config/routes.rb as the file a request must match before any controller, model or view runs.
Writing one route at a time
Writes single routes from a verb, a URL pattern and an action, and introduces dynamic segments like :username.
resources: seven routes, cashed in
Explains how resources creates seven actions in eight route entries, and how only: and param: change that.
Named routes: _path and _url helpers
Uses as: to name routes and generate _path and _url helpers, so a URL changes in one place.
The root route
Declares the root route for a bare slash and shows the boot error raised by a second root.
Seeing every route at once: bin/rails routes
Reads the four columns of bin/rails routes output, the optional (.:format) segment, and filtering with grep.
Putting it together: one request, traced
Traces the partners route through every lesson and shows where routing hands the request to the controller.