1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns=
"http://www.w3.org/1999/xhtml" lang=
"en" xml:
lang=
"en">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
5 <title>Rails Routing from the Outside In
</title>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
9 <link href=
"stylesheets/base.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
10 <link href=
"stylesheets/forms.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
11 <link href=
"stylesheets/more.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
12 <style type=
"text/css">
36 border: solid
1px #cccccc;
52 #content dt tt, #content pre tt {
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
108 .left-floaty, .right-floaty {
120 .admonitionblock .icon {
124 .admonitionblock .content {
125 border: solid
1px #ffda78;
132 .admonitionblock .title {
134 margin-bottom:
0.5em;
138 border: solid
1px #aaaaff;
156 border: solid
1px #ccccbb;
161 .sidebarblock .sidebar-title {
164 margin-bottom:
0.3em;
167 .sidebarblock .sidebar-content
> .para:last-child
> p {
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
184 <a href=
"index.html" title=
"Ruby on Rails"><img src=
"images/rails_logo_remix.gif" alt=
"Rails" height=
"140" width=
"110" /></a>
187 <h1 id=
"site_title"><span>Ruby on Rails
</span></h1>
188 <h2 id=
"site_title_tagline">Sustainable productivity for web-application development
</h2>
191 <li class=
"first-child"><a href=
"http://www.rubyonrails.org/" title=
"Ruby on Rails" class=
"ruby_on_rails">Ruby on Rails
</a></li>
192 <li><a class=
"manuals" href=
"index.html" title=
"Manuals Index">Guides Index
</a></li>
202 <a href=
"#_the_dual_purpose_of_routing">The Dual Purpose of Routing
</a>
205 <li><a href=
"#_connecting_urls_to_code">Connecting URLs to Code
</a></li>
207 <li><a href=
"#_generating_urls_from_code">Generating URLs from Code
</a></li>
212 <a href=
"#_quick_tour_of_routes_rb">Quick Tour of Routes.rb
</a>
215 <li><a href=
"#_processing_the_file">Processing the File
</a></li>
217 <li><a href=
"#_restful_routes">RESTful Routes
</a></li>
219 <li><a href=
"#_named_routes">Named Routes
</a></li>
221 <li><a href=
"#_nested_routes">Nested Routes
</a></li>
223 <li><a href=
"#_regular_routes">Regular Routes
</a></li>
225 <li><a href=
"#_default_routes">Default Routes
</a></li>
230 <a href=
"#_restful_routing_the_rails_default">RESTful Routing: the Rails Default
</a>
233 <li><a href=
"#_what_is_rest">What is REST?
</a></li>
235 <li><a href=
"#_crud_verbs_and_actions">CRUD, Verbs, and Actions
</a></li>
237 <li><a href=
"#_urls_and_paths">URLs and Paths
</a></li>
239 <li><a href=
"#_defining_multiple_resources_at_the_same_time">Defining Multiple Resources at the Same Time
</a></li>
241 <li><a href=
"#_singular_resources">Singular Resources
</a></li>
243 <li><a href=
"#_customizing_resources">Customizing Resources
</a></li>
245 <li><a href=
"#_controller_namespaces_and_routing">Controller Namespaces and Routing
</a></li>
247 <li><a href=
"#_nested_resources">Nested Resources
</a></li>
249 <li><a href=
"#_route_generation_from_arrays">Route Generation from Arrays
</a></li>
251 <li><a href=
"#_namespaced_resources">Namespaced Resources
</a></li>
253 <li><a href=
"#_adding_more_restful_actions">Adding More RESTful Actions
</a></li>
258 <a href=
"#_regular_routes_2">Regular Routes
</a>
261 <li><a href=
"#_bound_parameters">Bound Parameters
</a></li>
263 <li><a href=
"#_wildcard_components">Wildcard Components
</a></li>
265 <li><a href=
"#_static_text">Static Text
</a></li>
267 <li><a href=
"#_querystring_parameters">Querystring Parameters
</a></li>
269 <li><a href=
"#_defining_defaults">Defining Defaults
</a></li>
271 <li><a href=
"#_named_routes_2">Named Routes
</a></li>
273 <li><a href=
"#_route_requirements">Route Requirements
</a></li>
275 <li><a href=
"#_route_conditions">Route Conditions
</a></li>
277 <li><a href=
"#_route_globbing">Route Globbing
</a></li>
279 <li><a href=
"#_route_options">Route Options
</a></li>
284 <a href=
"#_formats_and_respond_to">Formats and respond_to
</a>
287 <li><a href=
"#_specifying_the_format_with_an_http_header">Specifying the Format with an HTTP Header
</a></li>
289 <li><a href=
"#_recognized_mime_types">Recognized MIME types
</a></li>
294 <a href=
"#_the_default_routes">The Default Routes
</a>
297 <a href=
"#_the_empty_route">The Empty Route
</a>
300 <li><a href=
"#_using_map_root">Using map.root
</a></li>
302 <li><a href=
"#_connecting_the_empty_string">Connecting the Empty String
</a></li>
307 <a href=
"#_inspecting_and_testing_routes">Inspecting and Testing Routes
</a>
310 <li><a href=
"#_seeing_existing_routes_with_rake">Seeing Existing Routes with rake
</a></li>
312 <li><a href=
"#_testing_routes">Testing Routes
</a></li>
317 <a href=
"#_changelog">Changelog
</a>
323 <h1>Rails Routing from the Outside In
</h1>
325 <div class=
"sectionbody">
326 <div class=
"para"><p>This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:
</p></div>
327 <div class=
"ilist"><ul>
330 Understand the purpose of routing
335 Decipher the code in
<tt>routes.rb
</tt>
340 Construct your own routes, using either the classic hash style or the now-preferred RESTful style
345 Identify how a route will map to a controller and action
351 <h2 id=
"_the_dual_purpose_of_routing">1. The Dual Purpose of Routing
</h2>
352 <div class=
"sectionbody">
353 <div class=
"para"><p>Rails routing is a two-way piece of machinery - rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
</p></div>
354 <h3 id=
"_connecting_urls_to_code">1.1. Connecting URLs to Code
</h3>
355 <div class=
"para"><p>When your Rails application receives an incoming HTTP request, say
</p></div>
356 <div class=
"listingblock">
357 <div class=
"content">
358 <pre><tt>GET /patients/
17</tt></pre>
360 <div class=
"para"><p>the routing engine within Rails is the piece of code that dispatches the request to the appropriate spot in your application. In this case, the application would most likely end up running the
<tt>show
</tt> action within the
<tt>patients
</tt> controller, displaying the details of the patient whose ID is
17.
</p></div>
361 <h3 id=
"_generating_urls_from_code">1.2. Generating URLs from Code
</h3>
362 <div class=
"para"><p>Routing also works in reverse. If your application contains this code:
</p></div>
363 <div class=
"listingblock">
364 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
366 http://www.lorenzobettini.it
367 http://www.gnu.org/software/src-highlite -->
368 <pre><tt><span style=
"color: #009900">@patient
</span> <span style=
"color: #990000">=
</span> Patient
<span style=
"color: #990000">.
</span>find
<span style=
"color: #990000">(
</span><span style=
"color: #993399">17</span><span style=
"color: #990000">)
</span>
369 <span style=
"color: #FF0000"><%= link_to
"Patient Record", patient_path(@patient) %
></span>
370 </tt></pre></div></div>
371 <div class=
"para"><p>Then the routing engine is the piece that translates that to a link to a URL such as
<tt>http://example.com/patients/
17</tt>. By using routing in this way, you can reduce the brittleness of your application as compared to one with hard-coded URLs, and make your code easier to read and understand.
</p></div>
372 <div class=
"admonitionblock">
375 <img src=
"./images/icons/note.png" alt=
"Note" />
377 <td class=
"content">Patient needs to be declared as a resource for this style of translation via a named route to be available.
</td>
381 <h2 id=
"_quick_tour_of_routes_rb">2. Quick Tour of Routes.rb
</h2>
382 <div class=
"sectionbody">
383 <div class=
"para"><p>There are two components to routing in Rails: the routing engine itself, which is supplied as part of Rails, and the file
<tt>config/routes.rb
</tt>, which contains the actual routes that will be used by your application. Learning exactly what you can put in
<tt>routes.rb
</tt> is the main topic of this guide, but before we dig in let's get a quick overview.
</p></div>
384 <h3 id=
"_processing_the_file">2.1. Processing the File
</h3>
385 <div class=
"para"><p>In format,
<tt>routes.rb
</tt> is nothing more than one big block sent to
<tt>ActionController::Routing::Routes.draw
</tt>. Within this block, you can have comments, but it's likely that most of your content will be individual lines of code - each line being a route in your application. You'll find five main types of content in this file:
</p></div>
386 <div class=
"ilist"><ul>
413 <div class=
"para"><p>Each of these types of route is covered in more detail later in this guide.
</p></div>
414 <div class=
"para"><p>The
<tt>routes.rb
</tt> file is processed from top to bottom when a request comes in. The request will be dispatched to the first matching route. If there is no matching route, then Rails returns HTTP status
404 to the caller.
</p></div>
415 <h3 id=
"_restful_routes">2.2. RESTful Routes
</h3>
416 <div class=
"para"><p>RESTful routes take advantage of the built-in REST orientation of Rails to wrap up a lot of routing information in a single declaration. A RESTful route looks like this:
</p></div>
417 <div class=
"listingblock">
418 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
420 http://www.lorenzobettini.it
421 http://www.gnu.org/software/src-highlite -->
422 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>books
423 </tt></pre></div></div>
424 <h3 id=
"_named_routes">2.3. Named Routes
</h3>
425 <div class=
"para"><p>Named routes give you very readable links in your code, as well as handling incoming requests. Here's a typical named route:
</p></div>
426 <div class=
"listingblock">
427 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
429 http://www.lorenzobettini.it
430 http://www.gnu.org/software/src-highlite -->
431 <pre><tt>map
<span style=
"color: #990000">.
</span>login
<span style=
"color: #FF0000">'/login'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'sessions'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'new'
</span>
432 </tt></pre></div></div>
433 <h3 id=
"_nested_routes">2.4. Nested Routes
</h3>
434 <div class=
"para"><p>Nested routes let you declare that one resource is contained within another resource. You'll see later on how this translates to URLs and paths in your code. For example, if your application includes parts, each of which belongs to an assembly, you might have this nested route declaration:
</p></div>
435 <div class=
"listingblock">
436 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
438 http://www.lorenzobettini.it
439 http://www.gnu.org/software/src-highlite -->
440 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>assemblies
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>assemblies
<span style=
"color: #990000">|
</span>
441 assemblies
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>parts
442 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
443 </tt></pre></div></div>
444 <h3 id=
"_regular_routes">2.5. Regular Routes
</h3>
445 <div class=
"para"><p>In many applications, you'll also see non-RESTful routing, which explicitly connects the parts of a URL to a particular action. For example,
</p></div>
446 <div class=
"listingblock">
447 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
449 http://www.lorenzobettini.it
450 http://www.gnu.org/software/src-highlite -->
451 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'parts/:number'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'inventory'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span>
452 </tt></pre></div></div>
453 <h3 id=
"_default_routes">2.6. Default Routes
</h3>
454 <div class=
"para"><p>The default routes are a safety net that catch otherwise-unrouted requests. Many Rails applications will contain this pair of default routes:
</p></div>
455 <div class=
"listingblock">
456 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
458 http://www.lorenzobettini.it
459 http://www.gnu.org/software/src-highlite -->
460 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id'
</span>
461 map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id.:format'
</span>
462 </tt></pre></div></div>
463 <div class=
"para"><p>These default routes are automatically generated when you create a new Rails application. If you're using RESTful routing for everything in your application, you will probably want to remove them. But be sure you're not using the default routes before you remove them!
</p></div>
465 <h2 id=
"_restful_routing_the_rails_default">3. RESTful Routing: the Rails Default
</h2>
466 <div class=
"sectionbody">
467 <div class=
"para"><p>RESTful routing is the current standard for routing in Rails, and it's the one that you should prefer for new applications. It can take a little while to understand how RESTful routing works, but it's worth the effort; your code will be easier to read and you'll be working with Rails, rather than fighting against it, when you use this style of routing.
</p></div>
468 <h3 id=
"_what_is_rest">3.1. What is REST?
</h3>
469 <div class=
"para"><p>The foundation of RESTful routing is generally considered to be Roy Fielding's doctoral thesis,
<a href=
"http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures
</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:
</p></div>
470 <div class=
"ilist"><ul>
473 Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
478 Transferring representations of the state of that resource between system components.
482 <div class=
"para"><p>For example, to a Rails application a request such as this:
</p></div>
483 <div class=
"para"><p><tt>DELETE /photos/
17</tt></p></div>
484 <div class=
"para"><p>would be understood to refer to a photo resource with the ID of
17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.
</p></div>
485 <h3 id=
"_crud_verbs_and_actions">3.2. CRUD, Verbs, and Actions
</h3>
486 <div class=
"para"><p>In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as
</p></div>
487 <div class=
"listingblock">
488 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
490 http://www.lorenzobettini.it
491 http://www.gnu.org/software/src-highlite -->
492 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
493 </tt></pre></div></div>
494 <div class=
"para"><p>creates seven different routes in your application:
</p></div>
495 <div class=
"tableblock">
498 cellspacing=
"0" cellpadding=
"4">
538 display a list of all photos
555 return an HTML form for creating a new photo
589 display a specific photo
606 return an HTML form for editing a photo
623 update a specific photo
640 delete a specific photo
646 <div class=
"para"><p>For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as
<tt>params[:id]
</tt>.
</p></div>
647 <div class=
"admonitionblock">
650 <img src=
"./images/icons/tip.png" alt=
"Tip" />
652 <td class=
"content">If you consistently use RESTful routes in your application, you should disable the default routes in
<tt>routes.rb
</tt> so that Rails will enforce the mapping between HTTP verbs and routes.
</td>
655 <h3 id=
"_urls_and_paths">3.3. URLs and Paths
</h3>
656 <div class=
"para"><p>Creating a RESTful route will also make available a pile of helpers within your application:
</p></div>
657 <div class=
"ilist"><ul>
660 <tt>photos_url
</tt> and
<tt>photos_path
</tt> map to the path for the index and create actions
665 <tt>new_photo_url
</tt> and
<tt>new_photo_path
</tt> map to the path for the new action
670 <tt>edit_photo_url
</tt> and
<tt>edit_photo_path
</tt> map to the path for the edit action
675 <tt>photo_url
</tt> and
<tt>photo_path
</tt> map to the path for the show, update, and destroy actions
679 <div class=
"admonitionblock">
682 <img src=
"./images/icons/note.png" alt=
"Note" />
684 <td class=
"content">Because routing makes use of the HTTP verb as well as the path in the request to dispatch requests, the seven routes generated by a RESTful routing entry only give rise to four pairs of helpers.
</td>
687 <div class=
"para"><p>In each case, the
<tt>_url
</tt> helper generates a string containing the entire URL that the application will understand, while the
<tt>_path
</tt> helper generates a string containing the relative path from the root of the application. For example:
</p></div>
688 <div class=
"listingblock">
689 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
691 http://www.lorenzobettini.it
692 http://www.gnu.org/software/src-highlite -->
693 <pre><tt>photos_url
<span style=
"font-style: italic"><span style=
"color: #9A1900"># =
> "http://www.example.com/photos"</span></span>
694 photos_path
<span style=
"font-style: italic"><span style=
"color: #9A1900"># =
> "/photos"</span></span>
695 </tt></pre></div></div>
696 <h3 id=
"_defining_multiple_resources_at_the_same_time">3.4. Defining Multiple Resources at the Same Time
</h3>
697 <div class=
"para"><p>If you need to create routes for more than one RESTful resource, you can save a bit of typing by defining them all with a single call to
<tt>map.resources
</tt>:
</p></div>
698 <div class=
"listingblock">
699 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
701 http://www.lorenzobettini.it
702 http://www.gnu.org/software/src-highlite -->
703 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>books
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>videos
704 </tt></pre></div></div>
705 <div class=
"para"><p>This has exactly the same effect as
</p></div>
706 <div class=
"listingblock">
707 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
709 http://www.lorenzobettini.it
710 http://www.gnu.org/software/src-highlite -->
711 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
712 map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>books
713 map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>videos
714 </tt></pre></div></div>
715 <h3 id=
"_singular_resources">3.5. Singular Resources
</h3>
716 <div class=
"para"><p>You can also apply RESTful routing to singleton resources within your application. In this case, you use
<tt>map.resource
</tt> instead of
<tt>map.resources
</tt> and the route generation is slightly different. For example, a routing entry of
</p></div>
717 <div class=
"listingblock">
718 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
720 http://www.lorenzobettini.it
721 http://www.gnu.org/software/src-highlite -->
722 <pre><tt>map
<span style=
"color: #990000">.
</span>resource
<span style=
"color: #990000">:
</span>geocoder
723 </tt></pre></div></div>
724 <div class=
"para"><p>creates six different routes in your application:
</p></div>
725 <div class=
"tableblock">
728 cellspacing=
"0" cellpadding=
"4">
768 return an HTML form for creating the new geocoder
785 create the new geocoder
802 display the one and only geocoder resource
819 return an HTML form for editing the geocoder
836 update the one and only geocoder resource
853 delete the geocoder resource
859 <div class=
"admonitionblock">
862 <img src=
"./images/icons/note.png" alt=
"Note" />
864 <td class=
"content">Even though the name of the resource is singular in
<tt>routes.rb
</tt>, the matching controller is still plural.
</td>
867 <div class=
"para"><p>A singular RESTful route generates an abbreviated set of helpers:
</p></div>
868 <div class=
"ilist"><ul>
871 <tt>new_geocoder_url
</tt> and
<tt>new_geocoder_path
</tt> map to the path for the new action
876 <tt>edit_geocoder_url
</tt> and
<tt>edit_geocoder_path
</tt> map to the path for the edit action
881 <tt>geocoder_url
</tt> and
<tt>geocoder_path
</tt> map to the path for the create, show, update, and destroy actions
885 <h3 id=
"_customizing_resources">3.6. Customizing Resources
</h3>
886 <div class=
"para"><p>Although the conventions of RESTful routing are likely to be sufficient for many applications, there are a number of ways to customize the way that RESTful routes work. These options include:
</p></div>
887 <div class=
"ilist"><ul>
900 <tt>:requirements
</tt>
920 <tt>:path_prefix
</tt>
925 <tt>:name_prefix
</tt>
939 <div class=
"para"><p>You can also add additional routes via the
<tt>:member
</tt> and
<tt>:collection
</tt> options, which are discussed later in this guide.
</p></div>
940 <h4 id=
"_using_controller">3.6.1. Using :controller
</h4>
941 <div class=
"para"><p>The
<tt>:controller
</tt> option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry:
</p></div>
942 <div class=
"listingblock">
943 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
945 http://www.lorenzobettini.it
946 http://www.gnu.org/software/src-highlite -->
947 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"images"</span>
948 </tt></pre></div></div>
949 <div class=
"para"><p>will recognize incoming URLs containing
<tt>photo
</tt> but route the requests to the Images controller:
</p></div>
950 <div class=
"tableblock">
953 cellspacing=
"0" cellpadding=
"4">
993 display a list of all images
1010 return an HTML form for creating a new image
1044 display a specific image
1061 return an HTML form for editing a image
1078 update a specific image
1095 delete a specific image
1101 <div class=
"admonitionblock">
1104 <img src=
"./images/icons/note.png" alt=
"Note" />
1106 <td class=
"content">The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you'd still get
<tt>photos_path
</tt>,
<tt>new_photo_path
</tt>, and so on.
</td>
1109 <h3 id=
"_controller_namespaces_and_routing">3.7. Controller Namespaces and Routing
</h3>
1110 <div class=
"para"><p>Rails allows you to group your controllers into namespaces by saving them in folders underneath
<tt>app/controllers
</tt>. The
<tt>:controller
</tt> option provides a convenient way to use these routes. For example, you might have a resource whose controller is purely for admin users in the
<tt>admin
</tt> folder:
</p></div>
1111 <div class=
"listingblock">
1112 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1114 http://www.lorenzobettini.it
1115 http://www.gnu.org/software/src-highlite -->
1116 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>adminphotos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"admin/photos"</span>
1117 </tt></pre></div></div>
1118 <div class=
"para"><p>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
<tt>adminphoto_path
</tt> helper, and you follow a link generated with
<tt><%= link_to
"show", adminphoto(
1) %
></tt> you will end up on the view generated by
<tt>admin/photos/show
</tt> but you will also end up in the same place if you have
<tt><%= link_to
"show", {:controller
⇒ "photos", :action
⇒ "show"} %
></tt> because Rails will generate the show URL relative to the current URL.
</p></div>
1119 <div class=
"admonitionblock">
1122 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1124 <td class=
"content">If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name:
<tt><%= link_to
"show", {:controller
⇒ "/photos", :action
⇒ "show"} %
></tt></td>
1127 <div class=
"para"><p>You can also specify a controller namespace with the
<tt>:namespace
</tt> option instead of a path:
</p></div>
1128 <div class=
"listingblock">
1129 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1131 http://www.lorenzobettini.it
1132 http://www.gnu.org/software/src-highlite -->
1133 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>adminphotos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>namespace
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"admin"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span>
1134 </tt></pre></div></div>
1135 <div class=
"para"><p>This can be especially useful when combined with
<tt>with_options
</tt> to map multiple namespaced routes together:
</p></div>
1136 <div class=
"listingblock">
1137 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1139 http://www.lorenzobettini.it
1140 http://www.gnu.org/software/src-highlite -->
1141 <pre><tt>map
<span style=
"color: #990000">.
</span>with_options
<span style=
"color: #990000">(:
</span>namespace
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"admin"</span><span style=
"color: #990000">)
</span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>admin
<span style=
"color: #990000">|
</span>
1142 admin
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>videos
1143 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1144 </tt></pre></div></div>
1145 <div class=
"para"><p>That would give you routing for
<tt>admin/photos
</tt> and
<tt>admin/videos
</tt> controllers.
</p></div>
1146 <h4 id=
"_using_singular">3.7.1. Using :singular
</h4>
1147 <div class=
"para"><p>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
<tt>:singular
</tt> option:
</p></div>
1148 <div class=
"listingblock">
1149 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1151 http://www.lorenzobettini.it
1152 http://www.gnu.org/software/src-highlite -->
1153 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>teeth
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>singular
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"tooth"</span>
1154 </tt></pre></div></div>
1155 <div class=
"admonitionblock">
1158 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1160 <td class=
"content">Depending on the other code in your application, you may prefer to add additional rules to the
<tt>Inflector
</tt> class instead.
</td>
1163 <h4 id=
"_using_requirements">3.7.2. Using :requirements
</h4>
1164 <div class=
"para"><p>You an use the
<tt>:requirements
</tt> option in a RESTful route to impose a format on the implied
<tt>:id
</tt> parameter in the singular routes. For example:
</p></div>
1165 <div class=
"listingblock">
1166 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1168 http://www.lorenzobettini.it
1169 http://www.gnu.org/software/src-highlite -->
1170 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>requirements
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"color: #FF6600">/[A-Z][A-Z][
0-
9]+/
</span><span style=
"color: #FF0000">}
</span>
1171 </tt></pre></div></div>
1172 <div class=
"para"><p>This declaration constrains the
<tt>:id
</tt> parameter to match the supplied regular expression. So, in this case,
<tt>/photos/
1</tt> would no longer be recognized by this route, but
<tt>/photos/RR27
</tt> would.
</p></div>
1173 <h4 id=
"_using_conditions">3.7.3. Using :conditions
</h4>
1174 <div class=
"para"><p>Conditions in Rails routing are currently used only to set the HTTP verb for individual routes. Although in theory you can set this for RESTful routes, in practice there is no good reason to do so. (You'll learn more about conditions in the discussion of classic routing later in this guide.)
</p></div>
1175 <h4 id=
"_using_as">3.7.4. Using :as
</h4>
1176 <div class=
"para"><p>The
<tt>:as
</tt> option lets you override the normal naming for the actual generated paths. For example:
</p></div>
1177 <div class=
"listingblock">
1178 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1180 http://www.lorenzobettini.it
1181 http://www.gnu.org/software/src-highlite -->
1182 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>as
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"images"</span>
1183 </tt></pre></div></div>
1184 <div class=
"para"><p>will recognize incoming URLs containing
<tt>image
</tt> but route the requests to the Photos controller:
</p></div>
1185 <div class=
"tableblock">
1188 cellspacing=
"0" cellpadding=
"4">
1213 <tbody valign=
"top">
1228 display a list of all photos
1245 return an HTML form for creating a new photo
1279 display a specific photo
1296 return an HTML form for editing a photo
1313 update a specific photo
1330 delete a specific photo
1336 <div class=
"admonitionblock">
1339 <img src=
"./images/icons/note.png" alt=
"Note" />
1341 <td class=
"content">The helpers will be generated with the name of the resource, not the path name. So in this case, you'd still get
<tt>photos_path
</tt>,
<tt>new_photo_path
</tt>, and so on.
</td>
1344 <h4 id=
"_using_path_names">3.7.5. Using :path_names
</h4>
1345 <div class=
"para"><p>The
<tt>:path_names
</tt> option lets you override the automatically-generated
"new" and
"edit" segments in URLs:
</p></div>
1346 <div class=
"listingblock">
1347 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1349 http://www.lorenzobettini.it
1350 http://www.gnu.org/software/src-highlite -->
1351 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_names
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>new
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'make'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>edit
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'change'
</span> <span style=
"color: #FF0000">}
</span>
1352 </tt></pre></div></div>
1353 <div class=
"para"><p>This would cause the routing to recognize URLs such as
</p></div>
1354 <div class=
"listingblock">
1355 <div class=
"content">
1356 <pre><tt>/photos/make
1357 /photos/
1/change
</tt></pre>
1359 <div class=
"admonitionblock">
1362 <img src=
"./images/icons/note.png" alt=
"Note" />
1364 <td class=
"content">The actual action names aren't changed by this option; the two URLs show would still route to the new and edit actions.
</td>
1367 <div class=
"admonitionblock">
1370 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1372 <td class=
"content">If you find yourself wanting to change this option uniformly for all of your routes, you can set a default in your environment:
</td>
1375 <div class=
"listingblock">
1376 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1378 http://www.lorenzobettini.it
1379 http://www.gnu.org/software/src-highlite -->
1380 <pre><tt>config
<span style=
"color: #990000">.
</span>action_controller
<span style=
"color: #990000">.
</span>resources_path_names
<span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>new
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'make'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>edit
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'change'
</span> <span style=
"color: #FF0000">}
</span>
1381 </tt></pre></div></div>
1382 <h4 id=
"_using_path_prefix">3.7.6. Using :path_prefix
</h4>
1383 <div class=
"para"><p>The
<tt>:path_prefix
</tt> option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:
</p></div>
1384 <div class=
"listingblock">
1385 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1387 http://www.lorenzobettini.it
1388 http://www.gnu.org/software/src-highlite -->
1389 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'/photographers/:photographer_id'
</span>
1390 </tt></pre></div></div>
1391 <div class=
"para"><p>Routes recognized by this entry would include:
</p></div>
1392 <div class=
"listingblock">
1393 <div class=
"content">
1394 <pre><tt>/photographers/
1/photos/
2
1395 /photographers/
1/photos
</tt></pre>
1397 <div class=
"admonitionblock">
1400 <img src=
"./images/icons/note.png" alt=
"Note" />
1402 <td class=
"content">In most cases, it's simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.
</td>
1405 <div class=
"admonitionblock">
1408 <img src=
"./images/icons/note.png" alt=
"Note" />
1410 <td class=
"content">You can also use
<tt>:path_prefix
</tt> with non-RESTful routes.
</td>
1413 <h4 id=
"_using_name_prefix">3.7.7. Using :name_prefix
</h4>
1414 <div class=
"para"><p>You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use
<tt>:path_prefix
</tt> to map differently. For example:
</p></div>
1415 <div class=
"listingblock">
1416 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1418 http://www.lorenzobettini.it
1419 http://www.gnu.org/software/src-highlite -->
1420 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'/photographers/:photographer_id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photographer_'
</span>
1421 map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'/agencies/:agency_id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'agency_'
</span>
1422 </tt></pre></div></div>
1423 <div class=
"para"><p>This combination will give you route helpers such as
<tt>photographer_photos_path
</tt> and
<tt>agency_edit_photo_path
</tt> to use in your code.
</p></div>
1424 <div class=
"admonitionblock">
1427 <img src=
"./images/icons/note.png" alt=
"Note" />
1429 <td class=
"content">You can also use
<tt>:name_prefix
</tt> with non-RESTful routes.
</td>
1432 <h4 id=
"_using_only_and_except">3.7.8. Using :only and :except
</h4>
1433 <div class=
"para"><p>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
<tt>:only
</tt> and
<tt>:except
</tt> options to fine-tune this behavior. The
<tt>:only
</tt> option specifies that only certain routes should be generated:
</p></div>
1434 <div class=
"listingblock">
1435 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1437 http://www.lorenzobettini.it
1438 http://www.gnu.org/software/src-highlite -->
1439 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>only
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[:
</span>index
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>show
<span style=
"color: #990000">]
</span>
1440 </tt></pre></div></div>
1441 <div class=
"para"><p>With this declaration, a
<tt>GET
</tt> request to
<tt>/photos
</tt> would succeed, but a
<tt>POST
</tt> request to
<tt>/photos
</tt> (which would ordinarily be routed to the create action) will fail.
</p></div>
1442 <div class=
"para"><p>The
<tt>:except
</tt> option specifies a route or list of routes that should
<em>not
</em> be generated:
</p></div>
1443 <div class=
"listingblock">
1444 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1446 http://www.lorenzobettini.it
1447 http://www.gnu.org/software/src-highlite -->
1448 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>except
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>destroy
1449 </tt></pre></div></div>
1450 <div class=
"para"><p>In this case, all of the normal routes except the route for
<tt>destroy
</tt> (a
<tt>DELETE
</tt> request to
<tt>/photos/
<em>id
</em></tt>) will be generated.
</p></div>
1451 <div class=
"para"><p>In addition to an action or a list of actions, you can also supply the special symbols
<tt>:all
</tt> or
<tt>:none
</tt> to the
<tt>:only
</tt> and
<tt>:except
</tt> options.
</p></div>
1452 <div class=
"admonitionblock">
1455 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1457 <td class=
"content">If your application has many RESTful routes, using
<tt>:only
</tt> and
<tt>:accept
</tt> to generate only the routes that you actually need can cut down on memory use and speed up the routing process.
</td>
1460 <h3 id=
"_nested_resources">3.8. Nested Resources
</h3>
1461 <div class=
"para"><p>It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
</p></div>
1462 <div class=
"listingblock">
1463 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1465 http://www.lorenzobettini.it
1466 http://www.gnu.org/software/src-highlite -->
1467 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Magazine
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1468 has_many
<span style=
"color: #990000">:
</span>ads
1469 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1471 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Ad
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1472 belongs_to
<span style=
"color: #990000">:
</span>magazine
1473 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1474 </tt></pre></div></div>
1475 <div class=
"para"><p>Each ad is logically subservient to one magazine. Nested routes allow you to capture this relationship in your routing. In this case, you might include this route declaration:
</p></div>
1476 <div class=
"listingblock">
1477 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1479 http://www.lorenzobettini.it
1480 http://www.gnu.org/software/src-highlite -->
1481 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1482 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>ads
1483 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1484 </tt></pre></div></div>
1485 <div class=
"para"><p>In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:
</p></div>
1486 <div class=
"tableblock">
1489 cellspacing=
"0" cellpadding=
"4">
1514 <tbody valign=
"top">
1529 display a list of all ads for a specific magazine
1537 /magazines/
1/ads/new
1546 return an HTML form for creating a new ad belonging to a specific magazine
1563 create a new ad belonging to a specific magazine
1580 display a specific ad belonging to a specific magazine
1588 /magazines/
1/ads/
1/edit
1597 return an HTML form for editing an ad belonging to a specific magazine
1614 update a specific ad belonging to a specific magazine
1631 delete a specific ad belonging to a specific magazine
1637 <div class=
"para"><p>This will also create routing helpers such as
<tt>magazine_ads_url
</tt> and
<tt>edit_magazine_ad_path
</tt>.
</p></div>
1638 <h4 id=
"_using_name_prefix_2">3.8.1. Using :name_prefix
</h4>
1639 <div class=
"para"><p>The
<tt>:name_prefix
</tt> option overrides the automatically-generated prefix in nested route helpers. For example,
</p></div>
1640 <div class=
"listingblock">
1641 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1643 http://www.lorenzobettini.it
1644 http://www.gnu.org/software/src-highlite -->
1645 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1646 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>ads
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'periodical'
</span>
1647 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1648 </tt></pre></div></div>
1649 <div class=
"para"><p>This will create routing helpers such as
<tt>periodical_ads_url
</tt> and
<tt>periodical_edit_ad_path
</tt>. You can even use
<tt>:name_prefix
</tt> to suppress the prefix entirely:
</p></div>
1650 <div class=
"listingblock">
1651 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1653 http://www.lorenzobettini.it
1654 http://www.gnu.org/software/src-highlite -->
1655 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1656 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>ads
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">nil
</span></span>
1657 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1658 </tt></pre></div></div>
1659 <div class=
"para"><p>This will create routing helpers such as
<tt>ads_url
</tt> and
<tt>edit_ad_path
</tt>. Note that calling these will still require supplying an article id:
</p></div>
1660 <div class=
"listingblock">
1661 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1663 http://www.lorenzobettini.it
1664 http://www.gnu.org/software/src-highlite -->
1665 <pre><tt>ads_url
<span style=
"color: #990000">(
</span><span style=
"color: #009900">@magazine
</span><span style=
"color: #990000">)
</span>
1666 edit_ad_path
<span style=
"color: #990000">(
</span><span style=
"color: #009900">@magazine
</span><span style=
"color: #990000">,
</span> <span style=
"color: #009900">@ad
</span><span style=
"color: #990000">)
</span>
1667 </tt></pre></div></div>
1668 <h4 id=
"_using_has_one_and_has_many">3.8.2. Using :has_one and :has_many
</h4>
1669 <div class=
"para"><p>The
<tt>:has_one
</tt> and
<tt>:has_many
</tt> options provide a succinct notation for simple nested routes. Use
<tt>:has_one
</tt> to nest a singleton resource, or
<tt>:has_many
</tt> to nest a plural resource:
</p></div>
1670 <div class=
"listingblock">
1671 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1673 http://www.lorenzobettini.it
1674 http://www.gnu.org/software/src-highlite -->
1675 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>has_one
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>photographer
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>has_many
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[:
</span>publications
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>versions
<span style=
"color: #990000">]
</span>
1676 </tt></pre></div></div>
1677 <div class=
"para"><p>This has the same effect as this set of declarations:
</p></div>
1678 <div class=
"listingblock">
1679 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1681 http://www.lorenzobettini.it
1682 http://www.gnu.org/software/src-highlite -->
1683 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>photo
<span style=
"color: #990000">|
</span>
1684 photo
<span style=
"color: #990000">.
</span>resource
<span style=
"color: #990000">:
</span>photographer
1685 photo
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>publications
1686 photo
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>versions
1687 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1688 </tt></pre></div></div>
1689 <h4 id=
"_limits_to_nesting">3.8.3. Limits to Nesting
</h4>
1690 <div class=
"para"><p>You can nest resources within other nested resources if you like. For example:
</p></div>
1691 <div class=
"listingblock">
1692 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1694 http://www.lorenzobettini.it
1695 http://www.gnu.org/software/src-highlite -->
1696 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>publishers
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>publisher
<span style=
"color: #990000">|
</span>
1697 publisher
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1698 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
1699 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1700 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1701 </tt></pre></div></div>
1702 <div class=
"para"><p>However, without the use of
<tt>name_prefix
⇒ nil
</tt>, deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize URLs such as
</p></div>
1703 <div class=
"listingblock">
1704 <div class=
"content">
1705 <pre><tt>/publishers/
1/magazines/
2/photos/
3</tt></pre>
1707 <div class=
"para"><p>The corresponding route helper would be
<tt>publisher_magazine_photo_url
</tt>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular
<a href=
"http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article
</a> by Jamis Buck proposes a rule of thumb for good Rails design:
</p></div>
1708 <div class=
"para"><p><em>Resources should never be nested more than
1 level deep.
</em></p></div>
1709 <h4 id=
"_shallow_nesting">3.8.4. Shallow Nesting
</h4>
1710 <div class=
"para"><p>The
<tt>:shallow
</tt> option provides an elegant solution to the difficulties of deeply-nested routes. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an
<tt>:id
</tt> parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:
</p></div>
1711 <div class=
"listingblock">
1712 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1714 http://www.lorenzobettini.it
1715 http://www.gnu.org/software/src-highlite -->
1716 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>publishers
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>shallow
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>publisher
<span style=
"color: #990000">|
</span>
1717 publisher
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1718 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
1719 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1720 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1721 </tt></pre></div></div>
1722 <div class=
"para"><p>This will enable recognition of (among others) these routes:
</p></div>
1723 <div class=
"listingblock">
1724 <div class=
"content">
1725 <pre><tt>/publishers/
1 ==
> publisher_path(
1)
1726 /publishers/
1/magazines ==
> publisher_magazines_path(
1)
1727 /magazines/
2 ==
> magazine_path(
2)
1728 /magazines/
2/photos ==
> magazines_photos_path(
2)
1729 /photos/
3 ==
> photo_path(
3)
</tt></pre>
1731 <div class=
"para"><p>With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. If you like, you can combine shallow nesting with the
<tt>:has_one
</tt> and
<tt>:has_many
</tt> options:
</p></div>
1732 <div class=
"listingblock">
1733 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1735 http://www.lorenzobettini.it
1736 http://www.gnu.org/software/src-highlite -->
1737 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>publishers
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>has_many
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>magazines
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>photos
<span style=
"color: #FF0000">}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>shallow
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span>
1738 </tt></pre></div></div>
1739 <h3 id=
"_route_generation_from_arrays">3.9. Route Generation from Arrays
</h3>
1740 <div class=
"para"><p>In addition to using the generated routing helpers, Rails can also generate RESTful routes from an array of parameters. For example, suppose you have a set of routes generated with these entries in routes.rb:
</p></div>
1741 <div class=
"listingblock">
1742 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1744 http://www.lorenzobettini.it
1745 http://www.gnu.org/software/src-highlite -->
1746 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>magazines
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>magazine
<span style=
"color: #990000">|
</span>
1747 magazine
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>ads
1748 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1749 </tt></pre></div></div>
1750 <div class=
"para"><p>Rails will generate helpers such as magazine_ad_path that you can use in building links:
</p></div>
1751 <div class=
"listingblock">
1752 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1754 http://www.lorenzobettini.it
1755 http://www.gnu.org/software/src-highlite -->
1756 <pre><tt><span style=
"color: #FF0000"><%= link_to
"Ad details", magazine_ad_path(@magazine, @ad) %
></span>
1757 </tt></pre></div></div>
1758 <div class=
"para"><p>Another way to refer to the same route is with an array of objects:
</p></div>
1759 <div class=
"listingblock">
1760 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1762 http://www.lorenzobettini.it
1763 http://www.gnu.org/software/src-highlite -->
1764 <pre><tt><span style=
"color: #FF0000"><%= link_to
"Ad details", [@magazine, @ad] %
></span>
1765 </tt></pre></div></div>
1766 <div class=
"para"><p>This format is especially useful when you might not know until runtime which of several types of object will be used in a particular link.
</p></div>
1767 <h3 id=
"_namespaced_resources">3.10. Namespaced Resources
</h3>
1768 <div class=
"para"><p>It's possible to do some quite complex things by combining
<tt>:path_prefix
</tt> and
<tt>:name_prefix
</tt>. For example, you can use the combination of these two options to move administrative resources to their own folder in your application:
</p></div>
1769 <div class=
"listingblock">
1770 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1772 http://www.lorenzobettini.it
1773 http://www.gnu.org/software/src-highlite -->
1774 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin/photos'
</span>
1775 map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>tags
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin_photo_'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin/photos/:photo_id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin/photo_tags'
</span>
1776 map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>ratings
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>name_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin_photo_'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>path_prefix
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin/photos/:photo_id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'admin/photo_ratings'
</span>
1777 </tt></pre></div></div>
1778 <div class=
"para"><p>The good news is that if you find yourself using this level of complexity, you can stop. Rails supports
<em>namespaced resources
</em> to make placing resources in their own folder a snap. Here's the namespaced version of those same three routes:
</p></div>
1779 <div class=
"listingblock">
1780 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1782 http://www.lorenzobettini.it
1783 http://www.gnu.org/software/src-highlite -->
1784 <pre><tt>map
<span style=
"color: #990000">.
</span>namespace
<span style=
"color: #990000">(:
</span>admin
<span style=
"color: #990000">)
</span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>admin
<span style=
"color: #990000">|
</span>
1785 admin
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span>
1786 <span style=
"color: #990000">:
</span>has_many
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>tags
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>ratings
<span style=
"color: #FF0000">}
</span>
1787 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1788 </tt></pre></div></div>
1789 <div class=
"para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get
<tt>admin_photos_url
</tt> that expects to find an
<tt>Admin::PhotosController
</tt> and that matches
<tt>admin/photos
</tt>, and
<tt>admin_photos_ratings_path
</tt> that matches
<tt>/admin/photos/
<em>photo_id
</em>/ratings
</tt>, expecting to use
<tt>Admin::RatingsController
</tt>. Even though you're not specifying
<tt>path_prefix
</tt> explicitly, the routing code will calculate the appropriate
<tt>path_prefix
</tt> from the route nesting.
</p></div>
1790 <h3 id=
"_adding_more_restful_actions">3.11. Adding More RESTful Actions
</h3>
1791 <div class=
"para"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).
</p></div>
1792 <h4 id=
"_adding_member_routes">3.11.1. Adding Member Routes
</h4>
1793 <div class=
"para"><p>To add a member route, use the
<tt>:member
</tt> option:
</p></div>
1794 <div class=
"listingblock">
1795 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1797 http://www.lorenzobettini.it
1798 http://www.gnu.org/software/src-highlite -->
1799 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>member
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>preview
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>get
<span style=
"color: #FF0000">}
</span>
1800 </tt></pre></div></div>
1801 <div class=
"para"><p>This will enable Rails to recognize URLs such as
<tt>/photos/
1/preview
</tt> using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create a
<tt>preview_photo
</tt> route helper.
</p></div>
1802 <div class=
"para"><p>Within the hash of member routes, each route name specifies the HTTP verb that it will recognize. You can use
<tt>:get
</tt>,
<tt>:put
</tt>,
<tt>:post
</tt>,
<tt>:delete
</tt>, or
<tt>:any
</tt> here. You can also specify an array of methods, if you need more than one but you don't want to allow just anything:
</p></div>
1803 <div class=
"listingblock">
1804 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1806 http://www.lorenzobettini.it
1807 http://www.gnu.org/software/src-highlite -->
1808 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>member
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>prepare
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[:
</span>get
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #990000">]
</span> <span style=
"color: #FF0000">}
</span>
1809 </tt></pre></div></div>
1810 <h4 id=
"_adding_collection_routes">3.11.2. Adding Collection Routes
</h4>
1811 <div class=
"para"><p>To add a collection route, use the
<tt>:collection
</tt> option:
</p></div>
1812 <div class=
"listingblock">
1813 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1815 http://www.lorenzobettini.it
1816 http://www.gnu.org/software/src-highlite -->
1817 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>collection
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>search
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>get
<span style=
"color: #FF0000">}
</span>
1818 </tt></pre></div></div>
1819 <div class=
"para"><p>This will enable Rails to recognize URLs such as
<tt>/photos/search
</tt> using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create a
<tt>search_photos
</tt> route helper.
</p></div>
1820 <div class=
"para"><p>Just as with member routes, you can specify an array of methods for a collection route:
</p></div>
1821 <div class=
"listingblock">
1822 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1824 http://www.lorenzobettini.it
1825 http://www.gnu.org/software/src-highlite -->
1826 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>collection
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>search
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[:
</span>get
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #990000">]
</span> <span style=
"color: #FF0000">}
</span>
1827 </tt></pre></div></div>
1828 <h4 id=
"_adding_new_routes">3.11.3. Adding New Routes
</h4>
1829 <div class=
"para"><p>To add a new route (one that creates a new resource), use the
<tt>:new
</tt> option:
</p></div>
1830 <div class=
"listingblock">
1831 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1833 http://www.lorenzobettini.it
1834 http://www.gnu.org/software/src-highlite -->
1835 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>new
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>upload
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #FF0000">}
</span>
1836 </tt></pre></div></div>
1837 <div class=
"para"><p>This will enable Rails to recognize URLs such as
<tt>/photos/upload
</tt> using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create a
<tt>upload_photos
</tt> route helper.
</p></div>
1838 <div class=
"admonitionblock">
1841 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1843 <td class=
"content">If you want to redefine the verbs accepted by one of the standard actions, you can do so by explicitly mapping that action. For example:
</td>
1846 <div class=
"listingblock">
1847 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1849 http://www.lorenzobettini.it
1850 http://www.gnu.org/software/src-highlite -->
1851 <pre><tt>map
<span style=
"color: #990000">.
</span>resources
<span style=
"color: #990000">:
</span>photos
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>new
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>new
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>any
<span style=
"color: #FF0000">}
</span>
1852 </tt></pre></div></div>
1853 <div class=
"para"><p>This will allow the new action to be invoked by any request to
<tt>photos/new
</tt>, no matter what HTTP verb you use.
</p></div>
1854 <h4 id=
"_a_note_of_caution">3.11.4. A Note of Caution
</h4>
1855 <div class=
"para"><p>If you find yourself adding many extra actions to a RESTful route, it's time to stop and ask yourself whether you're disguising the presence of another resource that would be better split off on its own. When the
<tt>:member
</tt> and
<tt>:collection
</tt> hashes become a dumping-ground, RESTful routes lose the advantage of easy readability that is one of their strongest points.
</p></div>
1857 <h2 id=
"_regular_routes_2">4. Regular Routes
</h2>
1858 <div class=
"sectionbody">
1859 <div class=
"para"><p>In addition to RESTful routing, Rails supports regular routing - a way to map URLs to controllers and actions. With regular routing, you don't get the masses of routes automatically generated by RESTful routing. Instead, you must set up each route within your application separately.
</p></div>
1860 <div class=
"para"><p>While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing
<em>when possible
</em>, because it will make parts of your application easier to write. But there's no need to try to shoehorn every last piece of your application into a RESTful framework if that's not a good fit.
</p></div>
1861 <h3 id=
"_bound_parameters">4.1. Bound Parameters
</h3>
1862 <div class=
"para"><p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special:
<tt>:controller
</tt> maps to the name of a controller in your application, and
<tt>:action
</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:
</p></div>
1863 <div class=
"listingblock">
1864 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1866 http://www.lorenzobettini.it
1867 http://www.gnu.org/software/src-highlite -->
1868 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id'
</span>
1869 </tt></pre></div></div>
1870 <div class=
"para"><p>If an incoming request of
<tt>/photos/show/
1</tt> is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the
<tt>show
</tt> action of the
<tt>Photos
</tt> controller, and to make the final parameter (
1) available as
<tt>params[:id]
</tt>.
</p></div>
1871 <h3 id=
"_wildcard_components">4.2. Wildcard Components
</h3>
1872 <div class=
"para"><p>You can set up as many wildcard symbols within a regular route as you like. Anything other than
<tt>:controller
</tt> or
<tt>:action
</tt> will be available to the matching action as part of the params hash. So, if you set up this route:
</p></div>
1873 <div class=
"listingblock">
1874 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1876 http://www.lorenzobettini.it
1877 http://www.gnu.org/software/src-highlite -->
1878 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id/:user_id'
</span>
1879 </tt></pre></div></div>
1880 <div class=
"para"><p>An incoming URL of
<tt>/photos/show/
1/
2</tt> will be dispatched to the
<tt>show
</tt> action of the
<tt>Photos
</tt> controller.
<tt>params[:id]
</tt> will be set to
1, and
<tt>params[:user_id]
</tt> will be set to
2.
</p></div>
1881 <h3 id=
"_static_text">4.3. Static Text
</h3>
1882 <div class=
"para"><p>You can specify static text when creating a route. In this case, the static text is used only for matching the incoming requests:
</p></div>
1883 <div class=
"listingblock">
1884 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1886 http://www.lorenzobettini.it
1887 http://www.gnu.org/software/src-highlite -->
1888 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id/with_user/:user_id'
</span>
1889 </tt></pre></div></div>
1890 <div class=
"para"><p>This route would respond to URLs such as
<tt>/photos/show/
1/with_user/
2</tt>.
</p></div>
1891 <h3 id=
"_querystring_parameters">4.4. Querystring Parameters
</h3>
1892 <div class=
"para"><p>Rails routing automatically picks up querystring parameters and makes them available in the
<tt>params
</tt> hash. For example, with this route:
</p></div>
1893 <div class=
"listingblock">
1894 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1896 http://www.lorenzobettini.it
1897 http://www.gnu.org/software/src-highlite -->
1898 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id'
</span>
1899 </tt></pre></div></div>
1900 <div class=
"para"><p>An incoming URL of
<tt>/photos/show/
1?user_id=
2</tt> will be dispatched to the
<tt>show
</tt> action of the
<tt>Photos
</tt> controller.
<tt>params[:id]
</tt> will be set to
1, and
<tt>params[:user_id]
</tt> will be equal to
2.
</p></div>
1901 <h3 id=
"_defining_defaults">4.5. Defining Defaults
</h3>
1902 <div class=
"para"><p>You do not need to explicitly use the
<tt>:controller
</tt> and
<tt>:action
</tt> symbols within a route. You can supply defaults for these two parameters in a hash:
</p></div>
1903 <div class=
"listingblock">
1904 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1906 http://www.lorenzobettini.it
1907 http://www.gnu.org/software/src-highlite -->
1908 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/:id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span>
1909 </tt></pre></div></div>
1910 <div class=
"para"><p>With this route, an incoming URL of
<tt>/photos/
12</tt> would be dispatched to the
<tt>show
</tt> action within the
<tt>Photos
</tt> controller.
</p></div>
1911 <div class=
"para"><p>You an also define other defaults in a route by supplying a hash for the
<tt>:defaults
</tt> option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:
</p></div>
1912 <div class=
"listingblock">
1913 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1915 http://www.lorenzobettini.it
1916 http://www.gnu.org/software/src-highlite -->
1917 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/:id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>defaults
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>format
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'jpg'
</span> <span style=
"color: #FF0000">}
</span>
1918 </tt></pre></div></div>
1919 <div class=
"para"><p>With this route, an incoming URL of
<tt>photos/
12</tt> would be dispatched to the
<tt>show
</tt> action within the
<tt>Photos
</tt> controller, and
<tt>params[:format]
</tt> will be set to
<tt>jpg
</tt>.
</p></div>
1920 <h3 id=
"_named_routes_2">4.6. Named Routes
</h3>
1921 <div class=
"para"><p>Regular routes need not use the
<tt>connect
</tt> method. You can use any other name here to create a
<em>named route
</em>. For example,
</p></div>
1922 <div class=
"listingblock">
1923 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1925 http://www.lorenzobettini.it
1926 http://www.gnu.org/software/src-highlite -->
1927 <pre><tt>map
<span style=
"color: #990000">.
</span>logout
<span style=
"color: #FF0000">'/logout'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'sessions'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'destroy'
</span>
1928 </tt></pre></div></div>
1929 <div class=
"para"><p>This will do two things. First, requests to
<tt>/logout
</tt> will be sent to the
<tt>destroy
</tt> method of the
<tt>Sessions
</tt> controller. Second, Rails will maintain the
<tt>logout_path
</tt> and
<tt>logout_url
</tt> helpers for use within your code.
</p></div>
1930 <h3 id=
"_route_requirements">4.7. Route Requirements
</h3>
1931 <div class=
"para"><p>You can use the
<tt>:requirements
</tt> option to enforce a format for any parameter in a route:
</p></div>
1932 <div class=
"listingblock">
1933 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1935 http://www.lorenzobettini.it
1936 http://www.gnu.org/software/src-highlite -->
1937 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/:id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span><span style=
"color: #990000">,
</span>
1938 <span style=
"color: #990000">:
</span>requirements
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"color: #FF6600">/[A-Z]\d{
5}/
</span> <span style=
"color: #FF0000">}
</span>
1939 </tt></pre></div></div>
1940 <div class=
"para"><p>This route would respond to URLs such as
<tt>/photo/A12345
</tt>. You can more succinctly express the same route this way:
</p></div>
1941 <div class=
"listingblock">
1942 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1944 http://www.lorenzobettini.it
1945 http://www.gnu.org/software/src-highlite -->
1946 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/:id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span><span style=
"color: #990000">,
</span>
1947 <span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"color: #FF6600">/[A-Z]\d{
5}/
</span>
1948 </tt></pre></div></div>
1949 <h3 id=
"_route_conditions">4.8. Route Conditions
</h3>
1950 <div class=
"para"><p>Route conditions (introduced with the
<tt>:conditions
</tt> option) are designed to implement restrictions on routes. Currently, the only supported restriction is
<tt>:method
</tt>:
</p></div>
1951 <div class=
"listingblock">
1952 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1954 http://www.lorenzobettini.it
1955 http://www.gnu.org/software/src-highlite -->
1956 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/:id'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'show'
</span><span style=
"color: #990000">,
</span>
1957 <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>method
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>get
<span style=
"color: #FF0000">}
</span>
1958 </tt></pre></div></div>
1959 <div class=
"para"><p>As with conditions in RESTful routes, you can specify
<tt>:get
</tt>,
<tt>:post
</tt>,
<tt>:put
</tt>,
<tt>:delete
</tt>, or
<tt>:any
</tt> for the acceptable method.
</p></div>
1960 <h3 id=
"_route_globbing">4.9. Route Globbing
</h3>
1961 <div class=
"para"><p>Route globbing is a way to specify that a particular parameter (which must be the last parameter in the route) should be matched to all the remaining parts of a route. For example
</p></div>
1962 <div class=
"listingblock">
1963 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1965 http://www.lorenzobettini.it
1966 http://www.gnu.org/software/src-highlite -->
1967 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">'photo/*other'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photos'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'unknown'
</span><span style=
"color: #990000">,
</span>
1968 </tt></pre></div></div>
1969 <div class=
"para"><p>This route would match
<tt>photo/
12</tt> or
<tt>/photo/long/path/to/
12</tt> equally well, creating an array of path segments as the value of
<tt>params[:other]
</tt>.
</p></div>
1970 <h3 id=
"_route_options">4.10. Route Options
</h3>
1971 <div class=
"para"><p>You can use
<tt>:with_options
</tt> to simplify defining groups of similar routes:
</p></div>
1972 <div class=
"listingblock">
1973 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1975 http://www.lorenzobettini.it
1976 http://www.gnu.org/software/src-highlite -->
1977 <pre><tt>map
<span style=
"color: #990000">.
</span>with_options
<span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'photo'
</span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>photo
<span style=
"color: #990000">|
</span>
1978 photo
<span style=
"color: #990000">.
</span>list
<span style=
"color: #FF0000">''
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'index'
</span>
1979 photo
<span style=
"color: #990000">.
</span>delete
<span style=
"color: #FF0000">':id/delete'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'delete'
</span>
1980 photo
<span style=
"color: #990000">.
</span>edit
<span style=
"color: #FF0000">':id/edit'
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'edit'
</span>
1981 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1982 </tt></pre></div></div>
1983 <div class=
"para"><p>The importance of
<tt>map.with_options
</tt> has declined with the introduction of RESTful routes.
</p></div>
1985 <h2 id=
"_formats_and_respond_to">5. Formats and respond_to
</h2>
1986 <div class=
"sectionbody">
1987 <div class=
"para"><p>There's one more way in which routing can do different things depending on differences in the incoming HTTP request: by issuing a response that corresponds to what the request specifies that it will accept. In Rails routing, you can control this with the special
<tt>:format
</tt> parameter in the route.
</p></div>
1988 <div class=
"para"><p>For instance, consider the second of the default routes in the boilerplate
<tt>routes.rb
</tt> file:
</p></div>
1989 <div class=
"listingblock">
1990 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1992 http://www.lorenzobettini.it
1993 http://www.gnu.org/software/src-highlite -->
1994 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id.:format'
</span>
1995 </tt></pre></div></div>
1996 <div class=
"para"><p>This route matches requests such as
<tt>/photo/edit/
1.xml
</tt> or
<tt>/photo/show/
2.rss
</tt>. Within the appropriate action code, you can issue different responses depending on the requested format:
</p></div>
1997 <div class=
"listingblock">
1998 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2000 http://www.lorenzobettini.it
2001 http://www.gnu.org/software/src-highlite -->
2002 <pre><tt>respond_to
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>format
<span style=
"color: #990000">|
</span>
2003 format
<span style=
"color: #990000">.
</span>html
<span style=
"font-style: italic"><span style=
"color: #9A1900"># return the default template for HTML
</span></span>
2004 format
<span style=
"color: #990000">.
</span>xml
<span style=
"color: #FF0000">{
</span> render
<span style=
"color: #990000">:
</span>xml
<span style=
"color: #990000">=
></span> <span style=
"color: #009900">@photo
</span><span style=
"color: #990000">.
</span>to_xml
<span style=
"color: #FF0000">}
</span>
2005 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2006 </tt></pre></div></div>
2007 <h3 id=
"_specifying_the_format_with_an_http_header">5.1. Specifying the Format with an HTTP Header
</h3>
2008 <div class=
"para"><p>If there is no
<tt>:format
</tt> parameter in the route, Rails will automatically look at the HTTP Accept header to determine the desired format.
</p></div>
2009 <h3 id=
"_recognized_mime_types">5.2. Recognized MIME types
</h3>
2010 <div class=
"para"><p>By default, Rails recognizes
<tt>html
</tt>,
<tt>text
</tt>,
<tt>json
</tt>,
<tt>csv
</tt>,
<tt>xml
</tt>,
<tt>rss
</tt>,
<tt>atom
</tt>, and
<tt>yaml
</tt> as acceptable response types. If you need types beyond this, you can register them in your environment:
</p></div>
2011 <div class=
"listingblock">
2012 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2014 http://www.lorenzobettini.it
2015 http://www.gnu.org/software/src-highlite -->
2016 <pre><tt>Mime
<span style=
"color: #990000">::
</span>Type
<span style=
"color: #990000">.
</span>register
<span style=
"color: #FF0000">"image/jpg"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>jpg
2017 </tt></pre></div></div>
2019 <h2 id=
"_the_default_routes">6. The Default Routes
</h2>
2020 <div class=
"sectionbody">
2021 <div class=
"para"><p>When you create a new Rails application,
<tt>routes.rb
</tt> is initialized with two default routes:
</p></div>
2022 <div class=
"listingblock">
2023 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2025 http://www.lorenzobettini.it
2026 http://www.gnu.org/software/src-highlite -->
2027 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id'
</span>
2028 map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">':controller/:action/:id.:format'
</span>
2029 </tt></pre></div></div>
2030 <div class=
"para"><p>These routes provide reasonable defaults for many URLs, if you're not using RESTful routing.
</p></div>
2031 <div class=
"admonitionblock">
2034 <img src=
"./images/icons/note.png" alt=
"Note" />
2036 <td class=
"content">The default routes will make every action of every controller in your application accessible to GET requests. If you've designed your application to make consistent use of RESTful and named routes, you should comment out the default routes to prevent access to your controllers through the wrong verbs. If you've had the default routes enabled during development, though, you need to be sure that you haven't unwittingly depended on them somewhere in your application - otherwise you may find mysterious failures when you disable them.
</td>
2040 <h2 id=
"_the_empty_route">7. The Empty Route
</h2>
2041 <div class=
"sectionbody">
2042 <div class=
"para"><p>Don't confuse the default routes with the empty route. The empty route has one specific purpose: to route requests that come in to the root of the web site. For example, if your site is example.com, then requests to
<tt>http://example.com
</tt> or
<tt>http://example.com/
</tt> will be handled by the empty route.
</p></div>
2043 <h3 id=
"_using_map_root">7.1. Using map.root
</h3>
2044 <div class=
"para"><p>The preferred way to set up the empty route is with the
<tt>map.root
</tt> command:
</p></div>
2045 <div class=
"listingblock">
2046 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2048 http://www.lorenzobettini.it
2049 http://www.gnu.org/software/src-highlite -->
2050 <pre><tt>map
<span style=
"color: #990000">.
</span>root
<span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"pages"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"main"</span>
2051 </tt></pre></div></div>
2052 <div class=
"para"><p>The use of the
<tt>root
</tt> method tells Rails that this route applies to requests for the root of the site.
</p></div>
2053 <div class=
"para"><p>For better readability, you can specify an already-created route in your call to
<tt>map.root
</tt>:
</p></div>
2054 <div class=
"listingblock">
2055 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2057 http://www.lorenzobettini.it
2058 http://www.gnu.org/software/src-highlite -->
2059 <pre><tt>map
<span style=
"color: #990000">.
</span>index
<span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"pages"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"main"</span>
2060 map
<span style=
"color: #990000">.
</span>root
<span style=
"color: #990000">:
</span>index
2061 </tt></pre></div></div>
2062 <div class=
"para"><p>Because of the top-down processing of the file, the named route must be specified
<em>before
</em> the call to
<tt>map.root
</tt>.
</p></div>
2063 <h3 id=
"_connecting_the_empty_string">7.2. Connecting the Empty String
</h3>
2064 <div class=
"para"><p>You can also specify an empty route by explicitly connecting the empty string:
</p></div>
2065 <div class=
"listingblock">
2066 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2068 http://www.lorenzobettini.it
2069 http://www.gnu.org/software/src-highlite -->
2070 <pre><tt>map
<span style=
"color: #990000">.
</span>connect
<span style=
"color: #FF0000">''
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"pages"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"main"</span>
2071 </tt></pre></div></div>
2072 <div class=
"admonitionblock">
2075 <img src=
"./images/icons/tip.png" alt=
"Tip" />
2077 <td class=
"content">If the empty route does not seem to be working in your application, make sure that you have deleted the file
<tt>public/index.html
</tt> from your Rails tree.
</td>
2081 <h2 id=
"_inspecting_and_testing_routes">8. Inspecting and Testing Routes
</h2>
2082 <div class=
"sectionbody">
2083 <div class=
"para"><p>Routing in your application should not be a
"black box" that you never open. Rails offers built-in tools for both inspecting and testing routes.
</p></div>
2084 <h3 id=
"_seeing_existing_routes_with_rake">8.1. Seeing Existing Routes with rake
</h3>
2085 <div class=
"para"><p>If you want a complete list of all of the available routes in your application, run the
<tt>rake routes
</tt> command. This will dump all of your routes to the console, in the same order that they appear in
<tt>routes.rb
</tt>. For each route, you'll see:
</p></div>
2086 <div class=
"ilist"><ul>
2089 The route name (if any)
2094 The HTTP verb used (if the route doesn't respond to all verbs)
2104 The routing parameters that will be generated by this URL
2108 <div class=
"para"><p>For example, here's a small section of the
<tt>rake routes
</tt> output for a RESTful route:
</p></div>
2109 <div class=
"listingblock">
2110 <div class=
"content">
2111 <pre><tt> users GET /users {:controller=
>"users", :action=
>"index"}
2112 formatted_users GET /users.:format {:controller=
>"users", :action=
>"index"}
2113 POST /users {:controller=
>"users", :action=
>"create"}
2114 POST /users.:format {:controller=
>"users", :action=
>"create"}
</tt></pre>
2116 <div class=
"admonitionblock">
2119 <img src=
"./images/icons/tip.png" alt=
"Tip" />
2121 <td class=
"content">You'll find that the output from
<tt>rake routes
</tt> is much more readable if you widen your terminal window until the output lines don't wrap.
</td>
2124 <h3 id=
"_testing_routes">8.2. Testing Routes
</h3>
2125 <div class=
"para"><p>Routes should be included in your testing strategy (just like the rest of your application). Rails offers three
<a href=
"http://api.rubyonrails.com/classes/ActionController/Assertions/RoutingAssertions.html">built-in assertions
</a> designed to make testing routes simpler:
</p></div>
2126 <div class=
"ilist"><ul>
2129 <tt>assert_generates
</tt>
2134 <tt>assert_recognizes
</tt>
2139 <tt>assert_routing
</tt>
2143 <h4 id=
"_the_tt_assert_generates_tt_assertion">8.2.1. The
<tt>assert_generates
</tt> Assertion
</h4>
2144 <div class=
"para"><p>Use
<tt>assert_generates
</tt> to assert that a particular set of options generate a particular path. You can use this with default routes or custom routes
</p></div>
2145 <div class=
"listingblock">
2146 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2148 http://www.lorenzobettini.it
2149 http://www.gnu.org/software/src-highlite -->
2150 <pre><tt>assert_generates
<span style=
"color: #FF0000">"/photos/1"</span><span style=
"color: #990000">,
</span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"show"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"1"</span> <span style=
"color: #FF0000">}
</span>
2151 assert_generates
<span style=
"color: #FF0000">"/about"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"pages"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"about"</span>
2152 </tt></pre></div></div>
2153 <h4 id=
"_the_tt_assert_recognizes_tt_assertion">8.2.2. The
<tt>assert_recognizes
</tt> Assertion
</h4>
2154 <div class=
"para"><p>The
<tt>assert_recognizes
</tt> assertion is the inverse of
<tt>assert_generates
</tt>. It asserts that Rails recognizes the given path and routes it to a particular spot in your application.
</p></div>
2155 <div class=
"listingblock">
2156 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2158 http://www.lorenzobettini.it
2159 http://www.gnu.org/software/src-highlite -->
2160 <pre><tt>assert_recognizes
<span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"show"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"1"</span> <span style=
"color: #FF0000">}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #FF0000">"/photos/1"</span>
2161 </tt></pre></div></div>
2162 <div class=
"para"><p>You can supply a
<tt>:method
</tt> argument to specify the HTTP verb:
</p></div>
2163 <div class=
"listingblock">
2164 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2166 http://www.lorenzobettini.it
2167 http://www.gnu.org/software/src-highlite -->
2168 <pre><tt>assert_recognizes
<span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"create"</span> <span style=
"color: #FF0000">}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>path
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>method
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #FF0000">}
</span>
2169 </tt></pre></div></div>
2170 <div class=
"para"><p>You can also use the RESTful helpers to test recognition of a RESTful route:
</p></div>
2171 <div class=
"listingblock">
2172 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2174 http://www.lorenzobettini.it
2175 http://www.gnu.org/software/src-highlite -->
2176 <pre><tt>assert_recognizes new_photo_url
<span style=
"color: #990000">,
</span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>path
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>method
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #FF0000">}
</span>
2177 </tt></pre></div></div>
2178 <h4 id=
"_the_tt_assert_routing_tt_assertion">8.2.3. The
<tt>assert_routing
</tt> Assertion
</h4>
2179 <div class=
"para"><p>The
<tt>assert_routing
</tt> assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of
<tt>assert_generates
</tt> and
<tt>assert_recognizes
</tt>.
</p></div>
2180 <div class=
"listingblock">
2181 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2183 http://www.lorenzobettini.it
2184 http://www.gnu.org/software/src-highlite -->
2185 <pre><tt>assert_routing
<span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>path
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>method
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>post
<span style=
"color: #FF0000">}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #FF0000">{
</span> <span style=
"color: #990000">:
</span>controller
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"photos"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>action
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"create"</span> <span style=
"color: #FF0000">}
</span>
2186 </tt></pre></div></div>
2188 <h2 id=
"_changelog">9. Changelog
</h2>
2189 <div class=
"sectionbody">
2190 <div class=
"para"><p><a href=
"http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3">Lighthouse ticket
</a></p></div>
2191 <div class=
"ilist"><ul>
2194 October
4,
2008: Added additional detail on specifying verbs for resource member/collection routes , by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a>
2199 September
23,
2008: Added section on namespaced controllers and routing, by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a>
2204 September
10,
2008: initial version by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a>