diff --git a/railties/guides/assets/stylesheets/main.css b/railties/guides/assets/stylesheets/main.css index 7ccae2c87e..bab0b7a9d9 100644 --- a/railties/guides/assets/stylesheets/main.css +++ b/railties/guides/assets/stylesheets/main.css @@ -437,7 +437,7 @@ div.code_container, div.important, div.caution, div.warning, div.note, div.info /* Remove bottom margin of paragraphs in special boxes, otherwise they get a spurious blank area below with the box background. */ div.important p, div.caution p, div.warning p, div.note p, div.info p { - margin-bottom: 0px; + margin-bottom: 1em; } /* Edge Badge diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index cf71e700dc..32b1e30502 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -2,180 +2,111 @@ h2. Rails Routing from the Outside In This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to: -* Understand the purpose of routing -* Decipher the code in +routes.rb+ -* Construct your own routes, using either the @match@ method or the preferred RESTful style -* Identify how a route will map to a controller and action +* Understand the code in +routes.rb+ +* Construct your own routes, using either the preferred resourceful style or with the @match@ method +* Identify what parameters to expect an action to receive +* Automatically create URLs using route helpers +* Use advanced techniques such as constraints and Rack endpoints endprologue. -h3. The Dual Purpose of Routing +h3. The Purpose of the Rails Router -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. +The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate URLs, avoiding the need to hardcode URL strings in your views. h4. Connecting URLs to Code -When your Rails application receives an incoming HTTP request, say +When your Rails application receives an incoming request -
++ -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 +show+ action within the +patients+ controller, displaying the details of the patient whose ID is 17. +it asks the router to match it to a controller action. If the first matching route is + + +match "/patients/:id" => "patients#show" + + +the request is dispatched to the +patients+ controller's +show+ action with { :id => "17" } in +params+. h4. Generating URLs from Code -Routing also works in reverse. If your application contains this code: +You can also generate routes. If your application contains this code: @patient = Patient.find(17)GET /patients/17 -
++ -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. - -h4. CRUD, Verbs, and Actions - -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 +it asks the router to map it to a controller action. If the first matching route is resources :photos -creates seven different routes in your application: +Rails would dispatch that request to the +destroy+ method on the +photos+ controller with { :id => "17" } in +params+. -|_.HTTP verb|_.URL |_.controller|_.action |_.used for| -|GET |/photos |Photos |index |display a list of all photos| -|GET |/photos/new |Photos |new |return an HTML form for creating a new photo| -|POST |/photos |Photos |create |create a new photo| -|GET |/photos/1 |Photos |show |display a specific photo| -|GET |/photos/1/edit |Photos |edit |return an HTML form for editing a photo| -|PUT |/photos/1 |Photos |update |update a specific photo| -|DELETE |/photos/1 |Photos |destroy |delete a specific photo| +h4. CRUD, Verbs, and Actions -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 +params[:id]+. +In Rails, a resourceful route provides a mapping between HTTP verbs and URLs and controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as + + +resources :photos + + +creates seven different routes in your application, all mapping to the +Photos+ controller: + +|_. Verb |_.URL |_.action |_.used for| +|GET |/photos |index |display a list of all photos| +|GET |/photos/new |new |return an HTML form for creating a new photo| +|POST |/photos |create |create a new photo| +|GET |/photos/:id |show |display a specific photo| +|GET |/photos/:id/edit |edit |return an HTML form for editing a photo| +|PUT |/photos/:id |update |update a specific photo| +|DELETE |/photos/:id |destroy |delete a specific photo| h4. URLs and Paths -Creating a RESTful route will also make available a pile of helpers within your application, something that requires explicit mention otherwise: +Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of +resources :photos+: -* +photos_url+ and +photos_path+ map to the path for the index and create actions -* +new_photo_url+ and +new_photo_path+ map to the path for the new action -* +edit_photo_url+ and +edit_photo_path+ map to the path for the edit action -* +photo_url+ and +photo_path+ map to the path for the show, update, and destroy actions +* +photos_path+ returns +/photos+ +* +new_photo_path+ returns +/photos/new+ +* +edit_photo_path+ returns +/photos/edit+ +* +photo_path(id)+ returns +/photos/:id+ (for instance, +photo_path(10)+ returns +/photos/10+) -NOTE: 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. +Each of these helpers has a corresponding +_url+ helper (such as +photos_url+) which returns the same path prefixed with the current host, port and path prefix. -In each case, the +_url+ helper generates a string containing the entire URL that the application will understand, while the +_path+ helper generates a string containing the relative path from the root of the application. For example: - - -photos_url # => "http://www.example.com/photos" -photos_path # => "/photos" - +NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions. h4. Defining Multiple Resources at the Same Time -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 +resources+: +If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to +resources+: resources :photos, :books, :videos -This has exactly the same effect as +This works exactly the same as resources :photos @@ -185,205 +116,91 @@ resources :videos h4. Singular Resources -You can also apply RESTful routing to singleton resources within your application. In this case, you use +resource+ instead of +resources+ and the route generation is slightly different. For example, a routing entry of +Sometimes, you have a resource that clients always look up without referencing an ID. A common example, +/profile+ always shows the profile of the currently logged in user. In this case, you can use a singular resource to map +/profile+ (rather than +/profile/:id+) to the +show+ action. resource :geocoder -creates six different routes in your application: +creates six different routes in your application, all mapping to the +Geocoders+ controller: -|_.HTTP verb|_.URL |_.controller|_.action |_.used for| -|GET |/geocoder/new |Geocoders |new |return an HTML form for creating the new geocoder| -|POST |/geocoder |Geocoders |create |create the new geocoder| -|GET |/geocoder |Geocoders |show |display the one and only geocoder resource| -|GET |/geocoder/edit |Geocoders |edit |return an HTML form for editing the geocoder| -|PUT |/geocoder |Geocoders |update |update the one and only geocoder resource| -|DELETE |/geocoder |Geocoders |destroy |delete the geocoder resource| +|_. Verb |_.URL |_.action |_.used for| +|GET |/geocoder/new |new |return an HTML form for creating the geocoder| +|POST |/geocoder |create |create the new geocoder| +|GET |/geocoder |show |display the one and only geocoder resource| +|GET |/geocoder/edit |edit |return an HTML form for editing the geocoder| +|PUT |/geocoder |update |update the one and only geocoder resource| +|DELETE |/geocoder |destroy |delete the geocoder resource| -NOTE: Even though the name of the resource is singular in +routes.rb+, the matching controller is still plural. +NOTE: Because you might want to use the same controller for a singular route (+/account+) and a plural route (+/accounts/45+), singular resources map to plural controllers. -A singular RESTful route generates an abbreviated set of helpers: +A singular resourceful route generates these helpers: -* +new_geocoder_url+ and +new_geocoder_path+ map to the path for the new action -* +edit_geocoder_url+ and +edit_geocoder_path+ map to the path for the edit action -* +geocoder_url+ and +geocoder_path+ map to the path for the create, show, update, and destroy actions +* +new_geocoder_path+ returns +/geocoder/new+ +* +edit_geocoder_path+ returns +/geocoder/edit+ +* +geocoder_path+ returns +/geocoder+ -h4. Customizing Resources - -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: - -* +:controller+ -* +:singular+ -* +:constraints+ -* +:as+ -* +:path_names+ -* +:only+ -* +:except+ - -You can also add additional routes via the +member+ and +collection+ blocks, which are discussed later in this guide. - -h5. Using +:controller+ - -The +:controller+ option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry: - - -resources :photos, :controller => "images" - - -will recognize incoming URLs containing +photo+ but route the requests to the Images controller: - -|_.HTTP verb|_.URL |_.controller|_.action |_.used for| -|GET |/photos |Images |index |display a list of all images| -|GET |/photos/new |Images |new |return an HTML form for creating a new image| -|POST |/photos |Images |create |create a new image| -|GET |/photos/1 |Images |show |display a specific image| -|GET |/photos/1/edit |Images |edit |return an HTML form for editing an image| -|PUT |/photos/1 |Images |update |update a specific image| -|DELETE |/photos/1 |Images |destroy |delete a specific image| - -NOTE: 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 +photos_path+, +new_photo_path+, and so on. +As with plural resources, the same helpers ending in +_url+ will also include the host, port and path prefix. h4. Controller Namespaces and Routing -Rails allows you to group your controllers into namespaces by saving them in folders underneath +app/controllers+. The +:controller+ 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 +admin+ folder: +You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an +Admin::+ namespace. You would place these controllers under the +app/controllers/admin+ directory, and you can group them together in your router: -resources :photos, :controller => "admin/photos" - - -If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the +photo_path+ helper, and you follow a link generated with +<%= link_to "show", photo_path(1) %>+ you will end up on the view generated by +admin/photos/show+, but you will also end up in the same place if you have +<%= link_to "show", {:controller => "photos", :action => "show"} %>+ because Rails will generate the show URL relative to the current URL. - -TIP: If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: +<%= link_to "show", {:controller => "/photos", :action => "show"} %>+ - -You can also specify a controller namespace with the +namespace+ method instead of a path. This can be especially useful when mapping multiple namespaced routes together: - - -namespace :admin do - resources :photos, :videos +namespace "admin" do + resources :posts, :comments end -That would give you routing for +admin/photos+ and +admin/videos+ controllers. +This will create a number of routes for each of the +posts+ and +comments+ controller. For +Admin::PostsController+, Rails will create: -The difference between generating routes through +namespace+ and the +:controller+ key is that the +namespace+ will add +admin+ to the generated helpers as well, so the above route generates +admin_photos_path+. +|_. Verb |_.URL |_.action |_. helper | +|GET |/admin/photos |index | admin_photos_path | +|GET |/admin/photos/new |new | new_admin_photos_path | +|POST |/admin/photos |create | admin_photos_path | +|GET |/admin/photos/1 |show | admin_photo_path(id) | +|GET |/admin/photos/1/edit |edit | edit_admin_photo_path(id) | +|PUT |/admin/photos/1 |update | admin_photo_path(id) | +|DELETE |/admin/photos/1 |destroy | admin_photo_path(id) | -h5. Using +:singular+ - -If for some reason Rails isn't doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the +:singular+ option: +If you want to route +/photos+ (without the prefix +/admin+) to +Admin::PostsController+, you could use -resources :teeth, :singular => "tooth" - - -TIP: Depending on the other code in your application, you may prefer to add additional rules to the +Inflector+ class instead. - -h5. Using +:constraints+ - -You can use the +:constraints+ option in a RESTful route to impose a format on the implied parameter in routes. For example: - - -resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/} - - -This declaration constrains the +:id+ parameter to match the supplied regular expression. So, in this case, +/photos/1+ would no longer be recognized by this route, but +/photos/RR27+ would. - -h5. Using +:as+ - -The +:as+ option lets you override the normal naming for the actual generated paths. For example: - - -resources :photos, :as => "images" - - -will recognize incoming URLs containing +image+ but route the requests to the Photos controller: - -|_.HTTP verb|_.URL |_.controller|_.action |_:used for| -|GET |/images |Photos |index |display a list of all photos| -|GET |/images/new |Photos |new |return an HTML form for creating a new photo| -|POST |/images |Photos |create |create a new photo| -|GET |/images/1 |Photos |show |display a specific photo| -|GET |/images/1/edit |Photos |edit |return an HTML form for editing a photo| -|PUT |/images/1 |Photos |update |update a specific photo| -|DELETE |/images/1 |Photos |destroy |delete a specific photo| - -NOTE: The helpers will be generated with the name of the resource, not the path name. So in this case, you'd still get +photos_path+, +new_photo_path+, and so on. - -h5. Using +:path_names+ - -The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in URLs: - - -resources :photos, :path_names => { :new => 'make', :edit => 'change' } - - -This would cause the routing to recognize URLs such as - -DELETE /photos/17 -
-/photos/make -/photos/1/change -- -NOTE: The actual action names aren't changed by this option; the two URLs shown would still route to the new and edit actions. - -TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can set a default in your environment: - - -config.action_controller.resources_path_names = { :new => 'make', :edit => 'change' } - - -h5. Using +:name_prefix+ - -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 +:path_prefix+ to map differently. For example: - - -resources :photos :name_prefix => 'photographer' - - -This combination will give you route helpers such as +photographer_photos_path+ to use in your code. - -NOTE: You can also use +:name_prefix+ with non-RESTful routes. - -h5. Using +:only+ and +:except+ - -By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option specifies that only certain routes should be generated: - - -resources :photos, :only => [:index, :show] - - -With this declaration, a +GET+ request to +/photos+ would succeed, but a +POST+ request to +/photos+ (which would ordinarily be routed to the create action) will fail. - -The +:except+ option specifies a route or list of routes that should _not_ be generated: - - -resources :photos, :except => :destroy - - -In this case, all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/id+) will be generated. - -TIP: If your application has many RESTful routes, using +:only+ and +:except+ to generate only the routes that you actually need can cut down on memory use and speed up the routing process. - -h5. Changing Path Names for Resources - -Using +scope+, we can alter path names generated by resources: - - -scope(:resources_path_names => { :new => "neu", :edit => "bearbeiten" }) do - resources :categories, :path => "kategorien" +scope :module => "admin" do + resources :posts, :comments end -With +scope+ defined, it now generates routes with customized path names. +or, for a single case -|_.HTTP verb|_.URL |_.controller |_.action |_:used for| -|GET |/kategorien |Categories |index |display a list of all categories| -|GET |/kategorien/neu |Categories |new |return an HTML form for creating a new category| -|POST |/kategorien |Categories |create |create a new category| -|GET |/kategorien/1 |Categories |show |display a specific category| -|GET |/kategorien/:id/bearbeiten |Categories |edit |return an HTML form for editing a category| -|PUT |/kategorien/1 |Categories |update |update a specific category| -|DELETE |/kategorien/1 |Categories |destroy |delete a specific category| + +resources :posts, :module => "admin" + + +If you want to route +/admin/photos+ to +PostsController+ (without the +Admin::+ module prefix), you could use + + +scope "/admin" do + resources :posts, :comments +end + + +or, for a single case + + +resources :posts, :path => "/admin" + + +In each of these cases, the named routes remain the same as if you did not use +scope+. In the last case, the following URLs map to +PostsController+: + +|_. Verb |_.URL |_.action |_. helper | +|GET |photos |index | photos_path | +|GET |photos/new |new | photos_path | +|POST |photos |create | photos_path | +|GET |photos/1 |show | photo_path(id) | +|GET |photos/1/edit |edit | dmin_photo_path(id) | +|PUT |photos/1 |update | photo_path(id) | +|DELETE |photos/1 |destroy | photo_path(id) | h4. Nested Resources @@ -399,7 +216,7 @@ class Ad < ActiveRecord::Base end -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: +Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration: resources :magazines do @@ -407,31 +224,19 @@ resources :magazines do end -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: +In addition to the routes for magazines, this declaration will also route ads to an +AdsController+. The ad URLs require a magazine: -|_.HTTP verb|_.URL |_.controller|_.action |_.used for| -|GET |/magazines/1/ads |Ads |index |display a list of all ads for a specific magazine| -|GET |/magazines/1/ads/new |Ads |new |return an HTML form for creating a new ad belonging to a specific magazine| -|POST |/magazines/1/ads |Ads |create |create a new ad belonging to a specific magazine| -|GET |/magazines/1/ads/1 |Ads |show |display a specific ad belonging to a specific magazine| -|GET |/magazines/1/ads/1/edit |Ads |edit |return an HTML form for editing an ad belonging to a specific magazine| -|PUT |/magazines/1/ads/1 |Ads |update |update a specific ad belonging to a specific magazine| -|DELETE |/magazines/1/ads/1 |Ads |destroy |delete a specific ad belonging to a specific magazine| +|_.Verb |_.URL |_.action |_.used for| +|GET |/magazines/1/ads |index |display a list of all ads for a specific magazine| +|GET |/magazines/1/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine| +|POST |/magazines/1/ads |create |create a new ad belonging to a specific magazine| +|GET |/magazines/1/ads/1 |show |display a specific ad belonging to a specific magazine| +|GET |/magazines/1/ads/1/edit |edit |return an HTML form for editing an ad belonging to a specific magazine| +|PUT |/magazines/1/ads/1 |update |update a specific ad belonging to a specific magazine| +|DELETE |/magazines/1/ads/1 |destroy |delete a specific ad belonging to a specific magazine| -This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+. - -h5(#nested-name-prefix). Using +:name_prefix+ - -The +:name_prefix+ option overrides the automatically-generated prefix in nested route helpers. For example, - - -resources :magazines do - resources :ads, :name_prefix => 'periodical' -end - - -This will create routing helpers such as +periodical_ads_url+ and +periodical_edit_ad_path+. +This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+). h5. Limits to Nesting @@ -455,33 +260,9 @@ The corresponding route helper would be +publisher_magazine_photo_url+, requirin TIP: _Resources should never be nested more than 1 level deep._ -h5. Shallow Nesting +h4. Creating URLs From Objects -The +:shallow+ 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 +:id+ parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes: - - -resources :publishers, :shallow => true do - resources :magazines do - resources :photos - end -end - - -This will enable recognition of (among others) these routes: - -
-/publishers/1 ==> publisher_path(1) -/publishers/1/magazines ==> publisher_magazines_path(1) -/magazines/2 ==> magazine_path(2) -/magazines/2/photos ==> magazines_photos_path(2) -/photos/3 ==> photo_path(3) -- -With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. - -h4. Route Generation from Arrays - -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: +In addition to using the routing helpers, Rails can also create URLs from an array of parameters. For example, suppose you have this set of routes: resources :magazines do @@ -489,49 +270,35 @@ resources :magazines do end -Rails will generate helpers such as magazine_ad_path that you can use in building links: +When using +magazine_ad_path+, you can pass in instances of +Magazine+ and +Ad+ instead of the numeric IDs. - +
+/photos/make +/photos/1/change ++ +NOTE: The actual action names aren't changed by this option. The two URLs shown would still route to the new and edit actions. + +TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope: + + +scope :path_names => { :new => "make" } do + # rest of your routes +end + + +h4. Overriding the Named Helper Prefix + +You can use the :name_prefix option to add a prefix to the named route helpers that Rails generates for a route. You can use this option to prevent collisions between routes using a path scope. + + +scope "admin" do + resources :photos, :name_prefix => "admin" +end + +resources :photos + + +This will provide route helpers such as +photographer_photos_path+. + +You could specify a name prefix to use for a group of routes in the scope: + + +scope "admin", :name_prefix => "admin" do + resources :photos, :accounts +end + +resources :photos, :accounts + + +NOTE: The +namespace+ scope will automatically add a +:name_prefix+ as well as +:module+ and +:path+ prefixes. + +h4. Restricting the Routes Created + +By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option tells Rails to create only the specified routes: + + +resources :photos, :only => [:index, :show] + + +Now, a +GET+ request to +/photos+ would succeed, but a +POST+ request to +/photos+ (which would ordinarily be routed to the +create+ action) will fail. + +The +:except+ option specifies a route or list of routes that Rails should _not_ create: + + +resources :photos, :except => :destroy + + +In this case, Rails will create all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/:id+). + +TIP: If your application has many RESTful routes, using +:only+ and +:except+ to generate only the routes that you actually need can cut down on memory use and speed up the routing process. + +h4. Translated Paths + +Using +scope+, we can alter path names generated by resources: + + +scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do + resources :categories, :path => "kategorien" +end + + +Rails now creates routes to the +CategoriesControlleR+. + +|_.HTTP verb|_.URL |_.action | +|GET |/kategorien |index | +|GET |/kategorien/neu |new | +|POST |/kategorien |create | +|GET |/kategorien/1 |show | +|GET |/kategorien/:id/bearbeiten |edit | +|PUT |/kategorien/1 |update | +|DELETE |/kategorien/1 |destroy | + +h4. Overriding the Singular Form + +If you want to customize the singular name of the route in the named helpers, you can use the +:singular+ option. + + +resources :teeth, :singular => "tooth" + + +TIP: If you want to define the singular form of a word for your entire application, you should add additional rules to the +Inflector+ instead. + +h4(#nested-name-prefix). Using +:name_prefix+ in Nested Resources + +The +:name_prefix+ option overrides the automatically-generated prefix for the parent resource in nested route helpers. For example, + + +resources :magazines do + resources :ads, :name_prefix => 'periodical' +end + + +This will create routing helpers such as +periodical_ads_url+ and +periodical_edit_ad_path+. h3. Inspecting and Testing Routes -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. +Rails offers facilities for inspecting and testing your routes. h4. Seeing Existing Routes with +rake+ -If you want a complete list of all of the available routes in your application, run the +rake routes+ command. This will dump all of your routes to the console, in the same order that they appear in +routes.rb+. For each route, you'll see: +If you want a complete list of all of the available routes in your application, run +rake routes+ command. This will print all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see: * The route name (if any) * The HTTP verb used (if the route doesn't respond to all verbs) -* The URL pattern -* The routing parameters that will be generated by this URL +* The URL pattern to match +* The routing parameters for the route For example, here's a small section of the +rake routes+ output for a RESTful route: @@ -812,7 +768,7 @@ You can supply a +:method+ argument to specify the HTTP verb: assert_recognizes({ :controller => "photos", :action => "create" }, { :path => "photos", :method => :post }) -You can also use the RESTful helpers to test recognition of a RESTful route: +You can also use the resourceful helpers to test recognition of a RESTful route: assert_recognizes new_photo_url, { :path => "photos", :method => :post } @@ -830,6 +786,7 @@ h3. Changelog "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3 +* April 10, 2010: Updated guide to remove outdated and superfluous information, and to provide information about new features, by "Yehuda Katz":http://www.yehudakatz.com * April 2, 2010: Updated guide to match new Routing DSL in Rails 3, by "Rizwan Reza":http://www.rizwanreza.com/ * Febuary 1, 2010: Modifies the routing documentation to match new routing DSL in Rails 3, by Prem Sichanugrist * October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes, by "Mike Gunderloy":credits.html#mgunderloy