diff options
Diffstat (limited to 'railties/guides/source/routing.textile')
-rw-r--r-- | railties/guides/source/routing.textile | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index 0823fb14e3..6f1677e85a 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -50,7 +50,7 @@ Resource routing allows you to quickly declare all of the common routes for a gi h4. Resources on the Web -Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. +Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PATCH+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. When your Rails application receives an incoming request for @@ -82,11 +82,10 @@ creates seven different routes in your application, all mapping to the +Photos+ |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 | +|PUT/PATCH |/photos/:id |update |update a specific photo | |DELETE |/photos/:id |destroy |delete a specific photo | - -NOTE: Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first. +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first. h4. Paths and URLs @@ -138,10 +137,10 @@ creates six different routes in your application, all mapping to the +Geocoders+ |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 | +|PUT/PATCH |/geocoder |update |update the one and only geocoder resource | |DELETE |/geocoder |destroy |delete the geocoder resource | -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. +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. 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 resourceful route generates these helpers: @@ -169,9 +168,11 @@ This will create a number of routes for each of the +posts+ and +comments+ contr |POST |/admin/posts |create | admin_posts_path | |GET |/admin/posts/:id |show | admin_post_path(:id) | |GET |/admin/posts/:id/edit |edit | edit_admin_post_path(:id) | -|PUT |/admin/posts/:id |update | admin_post_path(:id) | +|PUT/PATCH |/admin/posts/:id |update | admin_post_path(:id) | |DELETE |/admin/posts/:id |destroy | admin_post_path(:id) | +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. + If you want to route +/posts+ (without the prefix +/admin+) to +Admin::PostsController+, you could use <ruby> @@ -208,9 +209,11 @@ In each of these cases, the named routes remain the same as if you did not use + |POST |/admin/posts |create | posts_path | |GET |/admin/posts/:id |show | post_path(:id) | |GET |/admin/posts/:id/edit|edit | edit_post_path(:id)| -|PUT |/admin/posts/:id |update | post_path(:id) | +|PUT/PATCH |/admin/posts/:id |update | post_path(:id) | |DELETE |/admin/posts/:id |destroy | post_path(:id) | +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. + h4. Nested Resources It's common to have resources that are logically children of other resources. For example, suppose your application includes these models: @@ -235,15 +238,16 @@ end In addition to the routes for magazines, this declaration will also route ads to an +AdsController+. The ad URLs require a magazine: -|_.HTTP Verb |_.Path |_.action |_.used for | -|GET |/magazines/:id/ads |index |display a list of all ads for a specific magazine | -|GET |/magazines/:id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine | -|POST |/magazines/:id/ads |create |create a new ad belonging to a specific magazine | +|_.HTTP Verb |_.Path |_.action |_.used for | +|GET |/magazines/:id/ads |index |display a list of all ads for a specific magazine | +|GET |/magazines/:id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine | +|POST |/magazines/:id/ads |create |create a new ad belonging to a specific magazine | |GET |/magazines/:id/ads/:id |show |display a specific ad belonging to a specific magazine | |GET |/magazines/:id/ads/:id/edit |edit |return an HTML form for editing an ad belonging to a specific magazine | -|PUT |/magazines/:id/ads/:id |update |update a specific ad belonging to a specific magazine | +|PUT/PATCH |/magazines/:id/ads/:id |update |update a specific ad belonging to a specific magazine | |DELETE |/magazines/:id/ads/:id |destroy |delete a specific ad belonging to a specific magazine | +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. 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)+). @@ -323,7 +327,7 @@ end This will recognize +/photos/1/preview+ with GET, and route to the +preview+ action of +PhotosController+. It will also create the +preview_photo_url+ and +preview_photo_path+ helpers. -Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use +get+, +put+, +post+, or +delete+ here. If you don't have multiple +member+ routes, you can also pass +:on+ to a route, eliminating the block: +Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use +get+, +patch+, +put+, +post+, or +delete+ here. If you don't have multiple +member+ routes, you can also pass +:on+ to a route, eliminating the block: <ruby> resources :photos do @@ -642,7 +646,7 @@ will recognize incoming paths beginning with +/photos+ but route to the +Images+ |POST |/photos |create | photos_path | |GET |/photos/:id |show | photo_path(:id) | |GET |/photos/:id/edit |edit | edit_photo_path(:id) | -|PUT |/photos/:id |update | photo_path(:id) | +|PUT/PATCH |/photos/:id |update | photo_path(:id) | |DELETE |/photos/:id |destroy | photo_path(:id) | NOTE: Use +photos_path+, +new_photo_path+, etc. to generate paths for this resource. @@ -686,9 +690,11 @@ will recognize incoming paths beginning with +/photos+ and route the requests to |POST |/photos |create | images_path | |GET |/photos/:id |show | image_path(:id) | |GET |/photos/:id/edit |edit | edit_image_path(:id) | -|PUT |/photos/:id |update | image_path(:id) | +|PUT/PATCH |/photos/:id |update | image_path(:id) | |DELETE |/photos/:id |destroy | image_path(:id) | +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. + h4. Overriding the +new+ and +edit+ Segments The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in paths: @@ -790,9 +796,11 @@ Rails now creates routes to the +CategoriesController+. |POST |/kategorien |create | categories_path | |GET |/kategorien/:id |show | category_path(:id) | |GET |/kategorien/:id/bearbeiten |edit | edit_category_path(:id) | -|PUT |/kategorien/:id |update | category_path(:id) | +|PUT/PATCH |/kategorien/:id |update | category_path(:id) | |DELETE |/kategorien/:id |destroy | category_path(:id) | +NOTE: The HTTP Verb for the +update+ action defaults to +PUT+ for backwards compatibility. Set +config.default_method_for_update+ to +:patch+ to use +PATCH+. + h4. Overriding the Singular Form If you want to define the singular form of a resource, you should add additional rules to the +Inflector+. |