aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorMike Gunderloy <MikeG1@larkfarm.com>2008-09-08 07:11:44 -0500
committerMike Gunderloy <MikeG1@larkfarm.com>2008-09-08 07:11:44 -0500
commit0a9a80b2db4f86588400f1fc85f72eab95a9911a (patch)
treee2dfc8528e09d837d2a00022d3a3bc42ed261b8f /railties
parent310297e1592b0e7b77b86293df8ca63a3d964bf4 (diff)
downloadrails-0a9a80b2db4f86588400f1fc85f72eab95a9911a.tar.gz
rails-0a9a80b2db4f86588400f1fc85f72eab95a9911a.tar.bz2
rails-0a9a80b2db4f86588400f1fc85f72eab95a9911a.zip
Updates to nested/shallow routes discussion in "Routing from the Outside In" guide.
Added guidance on avoiding deeply-nested routes, further explanation of :shallow, show example of :has_many and :shallow together.
Diffstat (limited to 'railties')
-rw-r--r--railties/doc/guides/routing/routing_outside_in.txt21
1 files changed, 20 insertions, 1 deletions
diff --git a/railties/doc/guides/routing/routing_outside_in.txt b/railties/doc/guides/routing/routing_outside_in.txt
index aecfb949e9..13d8a8b732 100644
--- a/railties/doc/guides/routing/routing_outside_in.txt
+++ b/railties/doc/guides/routing/routing_outside_in.txt
@@ -455,7 +455,9 @@ However, without the use of +name_prefix => nil+, deeply-nested resources quickl
/publishers/1/magazines/2/photos/3
-------------------------------------------------------
-The corresponding route helper would be +publisher_magazine_photo_url+, requiring you to specify objects at all three levels.
+The corresponding route helper would be +publisher_magazine_photo_url+, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular link:http://weblog.jamisbuck.org/2007/2/5/nesting-resources[article] by Jamis Buck proposes a rule of thumb for good Rails design:
+
+_Resources should never be nested more than 1 level deep._
==== Shallow Nesting
@@ -479,6 +481,23 @@ This will enable recognition of (among others) these routes:
/magazines/2/photos ==> magazines_photos_path(2)
/photos/3 ==> photo_path(3)
-------------------------------------------------------
+
+With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with - but you _can_ supply more information. All of the nested routes continue to work, just as they would without shallow nesting, but less-deeply nested routes (even direct routes) work as well. So, with the declaration above, all of these routes refer to the same resource:
+
+-------------------------------------------------------
+/publishers/1/magazines/2/photos/3 ==> publisher_magazine_photo_path(1,2,3)
+/magazines/2/photos/3 ==> magazine_photo_path(2,3)
+/photos/3 ==> photo_path(3)
+-------------------------------------------------------
+
+Shallow nesting gives you the flexibility to use the shorter direct routes when you like, while still preserving the longer nested routes for times when they add code clarity.
+
+If you like, you can combine shallow nesting with the +:has_one+ and +:has_many+ options:
+
+[source, ruby]
+-------------------------------------------------------
+map.resources :publishers, :has_many => { :magazines => :photos }, :shallow => true
+-------------------------------------------------------
=== Adding More RESTful Actions