- The Two Purposes of Routing
- Bound Parameters
- Wildcard Components ("Receptors")
- Static Strings
- The routes.rb File
- The Ante-Default Route and respond_to
- The Empty Route
- Writing Custom Routes
- Using Static Strings
- Using Your Own "Receptors"
- A Note on Route Order
- Using Regular Expressions in Routes
- Default Parameters and the url_for Method
- Using Literal URLs
- Route Globbing
- Globbing Key-Value Pairs
- Named Routes
- What to Name Your Routes
- The Special Scope Method with_options
- Conclusion
Writing Custom Routes
The default route is a very general one. Its purpose is to catch all routes that haven't matched already. Now we're going to look at that already part: the routes defined earlier in the routes.rb file, routes that match more narrowly than the general one at the bottom of the file.
You've already seen the major components that you can put into a route: static strings, bound parameters (usually including :controller and often including :action), and wildcard "receptors" that get their values either positionally from a URL, or key-wise from a URL hash in your code.
When you write your routes, you have to think like the routing system.
- On the recognition side, that means your route has to have enough information in it—either hard-coded or waiting to receive values from the URL—to decide which controller and action to choose. (Or at least a controller; it can default to index if that's what you want.)
- On the generation side, your need to make sure that your hard-coded parameters and wildcards, taken together, provide you with enough values to pinpoint a route to use.
As long as these things are present—and as long as your routes are listed in order of priority ("fall-through" order)—your routes should work as desired.