From e5d6b82efbbc21c56316490e9355f0659ba89dea Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Wed, 12 Nov 2008 08:27:47 -0600 Subject: Add :except and :only documentation to the Routing Guide --- railties/doc/guides/source/routing_outside_in.txt | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'railties/doc') diff --git a/railties/doc/guides/source/routing_outside_in.txt b/railties/doc/guides/source/routing_outside_in.txt index a9ebb7bc36..1870ceff3e 100644 --- a/railties/doc/guides/source/routing_outside_in.txt +++ b/railties/doc/guides/source/routing_outside_in.txt @@ -229,6 +229,8 @@ Although the conventions of RESTful routing are likely to be sufficient for many * +:path_names+ * +:path_prefix+ * +:name_prefix+ +* +:only+ +* +:except+ You can also add additional routes via the +:member+ and +:collection+ options, which are discussed later in this guide. @@ -400,6 +402,30 @@ This combination will give you route helpers such as +photographer_photos_path+ NOTE: You can also use +:name_prefix+ with non-RESTful routes. +==== Using :only and :except + +By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option specifies that only certain routes should be generated: + +[source, ruby] +------------------------------------------------------- +map.resources :photos, :only => [:index, :show] +------------------------------------------------------- + +With this declaration, a +GET+ request to +/photos+ would succeed, but a +POST+ request to +/photos+ (which would ordinarily be routed to the create action) will fail. + +The +:except+ option specifies a route or list of routes that should _not_ be generated: + +[source, ruby] +------------------------------------------------------- +map.resources :photos, :except => :destroy +------------------------------------------------------- + +In this case, all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/_id_+) will be generated. + +In addition to an action or a list of actions, you can also supply the special symbols +:all+ or +:none+ to the +:only+ and +:accept+ options. + +TIP: If your application has many RESTful routes, using +:only+ and +:accept+ to generate only the routes that you actually need can cut down on memory use and speed up the routing process. + === Nested Resources It's common to have resources that are logically children of other resources. For example, suppose your application includes these models: -- cgit v1.2.3