Prev | Current Page 323 | Next

Brad Ediger

"Advanced Rails"

person_path(:id => 1, :section => 'enrollment')
=> "/people/1?section=enrollment"
Similarly, the default collection routes can also take a hash of query string
parameters:
>> app.people_path(:sort => 'last_name')
=> "/people?sort=last_name"
Routes can also be generated with a specific format; any of the formats specified in
Mime::Types (discussed later) are valid. There is another series of formatted_ named
routes for this purpose:
>> app.formatted_person_path(1, :js)
=> "/people/1.js"
>> app.formatted_person_path(1, :xml)
=> "/people/1.xml"
>> app.formatted_people_path(:rss)
=> "/people.rss"
This is useful in conjunction with respond_to, which switches responses based on the
content type accepted by the client. Note that, like the other named routes, if we want
custom query string parameters, we must convert all of the arguments to a hash:
>> app.formatted_person_path(:id => 1, :format => :xml,
:section => 'enrollment')
=> "/people/1.xml?section=enrollment"
Custom resource routes
The REST helpers also have provisions to create custom named routes pertaining
either to the collection (the parent resource) or the members of the collection (the children).
These are created with the :collection and :member options to map.resources,
respectively. Each new route must have a corresponding HTTPverb it is to be used
with. The routes directly correspond to actions on the controller.


Pages:
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335