Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Additional detail on routes for namespaced controllers.

  • Loading branch information...
commit 3da0c0a8424ce018fa756e906e824698dfdcb65e 1 parent 973bd54
@ffmike ffmike authored
Showing with 19 additions and 1 deletion.
  1. +19 −1 railties/doc/guides/routing/routing_outside_in.txt
View
20 railties/doc/guides/routing/routing_outside_in.txt
@@ -267,10 +267,28 @@ Rails allows you to group your controllers into namespaces by saving them in fol
map.resources :adminphotos, :controller => "admin/photos"
-------------------------------------------------------
-If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the +adminphoto_path+ helper, and you follow a link generated with +<%= link_to "show", adminphoto(1) %> you will end up on the view generated by +admin/photos/show+ but you will also end up in the same place if you have +<%= link_to "show", {:controller => "photos", :action => "show"} %>+ because Rails will generate the show URL relative to the current URL.
+If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the +adminphoto_path+ helper, and you follow a link generated with +<%= link_to "show", adminphoto(1) %>+ you will end up on the view generated by +admin/photos/show+ but you will also end up in the same place if you have +<%= link_to "show", {:controller => "photos", :action => "show"} %>+ because Rails will generate the show URL relative to the current URL.
TIP: If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: +<%= link_to "show", {:controller => "/photos", :action => "show"} %>+
+You can also specify a controller namespace with the +:namespace+ option instead of a path:
+
+[source, ruby]
+-------------------------------------------------------
+map.resources :adminphotos, :namespace => "admin", :controller => "photos"
+-------------------------------------------------------
+
+This can be especially useful when combined with +with_options+ to map multiple namespaced routes together:
+
+[source, ruby]
+-------------------------------------------------------
+map.with_options(:namespace => "admin") do |admin|
+ admin.resources :photos, :videos
+end
+-------------------------------------------------------
+
+That would give you routing for +admin/photos+ and +admin/videos+ controllers.
+
==== Using :singular
If for some reason Rails isn't doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the +:singular+ option:
Please sign in to comment.
Something went wrong with that request. Please try again.