O'Reilly logo

Ruby on Rails® for Microsoft Developers by Antonio Cangiano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.1. Defining Routes with map.connect

When you create a new application, Rails generates a config\routes.rb file that defines two default (unnamed or anonymous) routes:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

The routing subsystem will try to find a match between the defined patterns and the URL of the incoming request.

For example, take the first route into consideration:

map.connect ':controller/:action/:id'

This tells Rails that the route recognizes only paths that include an arbitrarily named controller, action, and id. Hence, '/library/borrow/25189' matches this route, and instructs Rails to map the first token in the URL (that is, library) to the :controller parameter, the second token (for example, borrow) to the :action parameter, and the third token to the :id parameter, which has a value of "25189." The params object will then look like this:

params = { :controller => "library", :action => "borrow", :id => "25189" }

Rails will therefore be able to process the request, by instantiating the library controller and executing the borrow action defined within it. As a developer, you'll be able to retrieve the :id parameter from within the controller through params[:id].

8.1.1. Default Parameters

/library/borrow/25189 results in the parameters { :controller => "library", :action=> "borrow", :id => "25189" }, but not all three parameters are strictly required.

You could omit an id, issuing perhaps a request for the path /library/catalog ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required