Prev | Current Page 327 | Next

Brad Ediger

"Advanced Rails"

An example would be a subscription
user??™s account, which contains billing information. This information is modeled as
a separate resource from the user??™s other data. In contrast to the collection
resource /users/1/posts and its member /users/1/posts/1, we have the singleton
resource /users/1/account.
Rails provides a way to map singleton resources with the map.resource statement (in
parallel to map.resources for collections of resources). The resource name is still singular,
but the inferred controller name is plural. Our routing statement for the preceding
example would be:
map.resources :users do |user|
user.resource :account
end
This code would expect an AccountsController to contain the user account actions.
Singleton resources contain the same methods as collection resources, but with no
index action.
216 | Chapter 7: REST, Resources, and Web Services
The named routes for singleton resources are similar to those for collections, again
missing only the route to the collection.
Depending on the application, not all of these routes may be used. For example, if
the account is created automatically when the user is created, then the new, create,
and destroy actions may not apply.
ActionView Support
The Rails link_to family of helpers can take a :method parameter to define the HTTP
method that will be used when a user clicks on the link. For a method of POST, PUT, or
DELETE, the helpers generate JavaScript that creates a hidden inline form, sets up the
appropriate hidden form field for the _method parameter (as is detailed next in
???Method emulation???), and submits it.


Pages:
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339