- 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
What to Name Your Routes
The best way to figure out what named routes you'll need is to think top-down; that is, think about what you want to write in your application code, and then create the routes that will make it possible.
Take, for example, this call to link_to:
<%= link_to "Auction of #{h(auction.item.description)}", :controller => "auctions", :action => "show", :id => auction.id %>
The routing rule to match that path is this (generic type of route):
map.connect "auctions/:id", :controller => "auctions", :action => "show"
It seems a little heavy-handed to spell out all the routing parameters again, just so that the routing system can figure out which route we want. And it sure would be nice to shorten that link_to code. After all, the routing rule already specifies the controller and action.
This is a good candidate for a named route. We can improve the situation by introducing auction_path:
<%= link_to "Auction for #{h(auction.item.description)}", auction_path(:id => auction.id) %>
Giving the route a name is a shortcut; it takes us straight to that route, without a long search and without having to provide a thick description of the route's hard-coded parameters.
The named route will be the same as the plain route—except that we replace "connect" with the name we want to give the route:
map.auction "auctions/:id", :controller => "auctions", :action => "show"
In the view, we can now use the more compact version of link_to; and we'll get (for auction 3, say) this URL in the hyperlink:
http://localhost:3000/auctions/show/3
Argument Sugar
In fact, we can make the argument to auction_path even shorter. If you need to supply an id number as an argument to a named route, you can just supply the number, without spelling out the :id key:
<%= link_to "Auction for #{h(auction.item.description)}", auction_path(auction.id) %>
And the syntactic sugar goes even further: You can just provide objects and Rails will grab the id automatically.
<%= link_to "Auction for #{h(auction.item.description)}", auction_path(auction) %>
This principle extends to other wildcards in the pattern string of the named route. For example, if you've got a route like this:
map.item 'auction/:auction_id/item/:id', :controller => "items", :action => "show"
you'd be able to call it like this:
<%= link to item.description, item_path(@auction, item) %>
and you'd get something like this as your path (depending on the exact id numbers):
/auction/5/item/11
Here, we're letting Rails infer the ids of both an auction object and an item object. As long as you provide the arguments in the order in which their ids occur in the route's pattern string, the correct values will be dropped into place in the generated path.
A Little More Sugar with Your Sugar?
Furthermore, it doesn't have to be the id value that the route generator inserts into the URL. You can override that value by defining a to_param method in your model.
Let's say you want the description of an item to appear in the URL for the auction on that item. In the item.rb model file, you would override to_params; here, we'll override it so that it provides a "munged" (stripped of punctuation and joined with hyphens) version of the description:
def to_param description.gsub(/\s/, "-").gsub([^\W-], '').downcase end
Subsequently, the method call item_path(@item) will produce something like this:
/auction/3/item/cello-bow
Of course, if you're putting things like "cello-bow" in a path field called :id, you will need to make provisions to dig the object out again. Blog applications that use this technique to create "slugs" for use in permanent links often have a separate database column to store the "munged" version of the title that serves as part of the path. That way, it's possible to do something like
Item.find_by_munged_description(params[:id])
to unearth the right item. (And yes, you can call it something other than :id in the route to make it clearer!)