This one-day course is an intensive, in-depth introduction to the routing system
in Rails. Participants will learn how the routing system works, and how to write
routing rules that streamline and clarify application design.
The routing system is the foundational layer for Rails's support of
REST-compliant program, and the course will also look closely at REST and how
Rails has incorporated RESTful principles into aspects of routing. Participants
will see, and try, examples of resource-based routing, and will go beyond the
boilerplate cases to understand how RESTful routing techniques can be customized
and fine-tuned to the advantage of programmers and code maintainers.
The two faces of routing
- Recognition: Determine what to do, given a URL
- Generation: Create a URL or path string
The routes.rb rules file
- A big code block
- "map": a Mapper object (a magic pen)
- Fall-through rule matching
- Rules (routes) and macros (batch routes and resources)
- Setting a top-level route
URL recognition
- Positional matching with pattern string
- Controller/action determination
- Regular expression constraints on matches
- Route globbing
- Route scoping
- The empty and default routes
Customizing routes for recognition
- Static substrings in the pattern string
- Symbol parameters
- Regular expression constraints
- Taking advantage of fall-through rules
URL/path generation
- In the views: link_to, form_for, and friends
- In the controllers: redirect_to
- The matching algorithm
- Defaulting to current request values
- URLs vs. paths
Named routes
- From missing method to method name
- Named routes with arguments
- Cleaning up views with named routes
- Sensible route naming
Testing and examining routes
- assert_generates
- assert_recognizes
- assert_routing
- Interpreting route information
- Examining routes in the console
- The rake routes task
REST: an introduction
- The key components of a RESTful system
- REST on the Web: returning to roots of HTTP
- Resources
- Representations
- REST compared with SOAP
REST in Rails: the principles
- Routing switching on HTTP request method
- Hard-coded action names
- Convention, convention, convention
REST in Rails: the practice
- Batch route creation with map resources
- The resource routing calculus
- Adding custom resource routes with :member and :collection
- Nested resources: how, why, why not
- Resource routing in forms
- Resource-based scaffolding: a honeypot
- Singleton resources (map resource)