Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate to advanced content levelIntermediate to advanced
479 pages
12h 55m
English
Wrox
Content preview from Ruby on Rails® for Microsoft Developers

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Michael Hartl

Publisher Resources

ISBN: 9780470374955Purchase book