Thus, Rails resource routing was born.
RESTful Rails | 211
Resource-Based Named Routes
Resource routing encapsulates all of the Rails CRUD actions into one routing
statement:
map.resources :cases
The resources method sets up a series of RESTful named routes. Just with that one
statement, you can now use link_to('View All', cases_path), link_to('Destroy',
case_path(@case), :method => :delete), and many more. This is more maintainable
because there are fewer named routes and they are standardized. It also encourages
you to model according to CRUD; you have to think carefully before adding ad-hoc
methods to controllers because each special case must be listed separately in the
route.
The following table shows the seven standard Rails CRUD actions, their semantics,
and the HTTPphrases that they correspond to. It assumes a routing declaration of
map.resources :people.
The controller name defaults to the resource name; in this case, the routing code
would look for a PeopleController. This can be changed; specifying map.resources
:people, :controller => 'members' would instead look for a MembersController.
By default, Rails generates four basic named routes for the map.resources :people
declaration. The four routes are shown below, along with their corresponding URIs
and the parameters that are passed back to the controller in the params hash when
the route is recognized.
Pages:
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332