Home > Articles > Programming > Ruby

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using Static Strings

Keep in mind that there's no necessary correspondence between the number of fields in the pattern string, the number of bound parameters, and the fact that every connection needs a controller and an action.

For example, you could write a route like this:

map.connect ":id", :controller => "auctions", :action => "show"

which would recognize a URL like this:

http://localhost:3000/8

The routing system would set params[:id] to 8 (based on the position of the :id "receptor," which matches the position of "8" in the URL), and it would execute the show action of the auctions controller. Of course, this is a bit of a stingy route, in terms of visual information. You might want to do something more like Listing 2.2, which is a little richer semantically-speaking:

map.connect "auctions/:id", :controller => "auctions", :action => "show"

This version of the route would recognize this:

http://localhost:3000/auctions/8

In this route, "auctions" is a static string. It will be looked for in the URL, for recognition purposes; and it will be inserted into the URL when you generate it with the following code:

<%= link_to "Auction details",
    :controller => "auctions",
    :action => "show",
    :id => auction.id %>
  • + Share This
  • 🔖 Save To Your Account