diff options
Diffstat (limited to 'railties/guides/source/routing.textile')
-rw-r--r-- | railties/guides/source/routing.textile | 114 |
1 files changed, 60 insertions, 54 deletions
diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index 1cbc5c8f6e..99dd9a1cd2 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -68,7 +68,7 @@ Rails would dispatch that request to the +destroy+ method on the +photos+ contro h4. CRUD, Verbs, and Actions -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 +In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as <ruby> resources :photos @@ -94,8 +94,8 @@ Creating a resourceful route will also expose a number of helpers to the control * +photos_path+ returns +/photos+ * +new_photo_path+ returns +/photos/new+ -* +edit_photo_path(id)+ returns +/photos/:id/edit+ (for instance, +edit_photo_path(10)+ returns +/photos/10/edit+) -* +photo_path(id)+ returns +/photos/:id+ (for instance, +photo_path(10)+ returns +/photos/10+) +* +edit_photo_path(:id)+ returns +/photos/:id/edit+ (for instance, +edit_photo_path(10)+ returns +/photos/10/edit+) +* +photo_path(:id)+ returns +/photos/:id+ (for instance, +photo_path(10)+ returns +/photos/10+) 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. @@ -163,14 +163,14 @@ end This will create a number of routes for each of the +posts+ and +comments+ controller. For +Admin::PostsController+, Rails will create: -|_.HTTP Verb |_.Path |_.action |_.named helper | -|GET |/admin/posts |index | admin_posts_path | -|GET |/admin/posts/new |new | new_admin_posts_path | -|POST |/admin/posts |create | admin_posts_path | -|GET |/admin/posts/1 |show | admin_post_path(id) | -|GET |/admin/posts/1/edit |edit | edit_admin_post_path(id) | -|PUT |/admin/posts/1 |update | admin_post_path(id) | -|DELETE |/admin/posts/1 |destroy | admin_post_path(id) | +|_.HTTP Verb |_.Path |_.action |_.named helper | +|GET |/admin/posts |index | admin_posts_path | +|GET |/admin/posts/new |new | new_admin_post_path | +|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) | +|DELETE |/admin/posts/:id |destroy | admin_post_path(:id) | If you want to route +/posts+ (without the prefix +/admin+) to +Admin::PostsController+, you could use @@ -204,12 +204,12 @@ In each of these cases, the named routes remain the same as if you did not use + |_.HTTP Verb |_.Path |_.action |_.named helper | |GET |/admin/posts |index | posts_path | -|GET |/admin/posts/new |new | posts_path | +|GET |/admin/posts/new |new | new_post_path | |POST |/admin/posts |create | posts_path | -|GET |/admin/posts/1 |show | post_path(id) | -|GET |/admin/posts/1/edit |edit | edit_post_path(id) | -|PUT |/admin/posts/1 |update | post_path(id) | -|DELETE |/admin/posts/1 |destroy | post_path(id) | +|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) | +|DELETE |/admin/posts/:id |destroy | post_path(:id) | h4. Nested Resources @@ -236,13 +236,13 @@ 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/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 | +|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 | +|DELETE |/magazines/:id/ads/:id |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+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+). @@ -560,13 +560,19 @@ would match +zoo/woo/foo/bar/baz+ with +params[:a]+ equals +"zoo/woo"+, and +par NOTE: Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route: <ruby> -map '*pages' => 'pages#show' +match '*pages' => 'pages#show' </ruby> NOTE: By requesting +"/foo/bar.json"+, your +params[:pages]+ will be equals to +"foo/bar"+ with the request format of JSON. If you want the old 3.0.x behavior back, you could supply +:format => false+ like this: <ruby> -map '*pages' => 'pages#show', :format => false +match '*pages' => 'pages#show', :format => false +</ruby> + +NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply +:format => true+ like this: + +<ruby> +match '*pages' => 'pages#show', :format => true </ruby> h4. Redirection @@ -628,16 +634,16 @@ resources :photos, :controller => "images" will recognize incoming paths beginning with +/photos+ but route to the +Images+ controller: -|_.HTTP Verb |_.Path |_.action |_.named helper | -|GET |/photos |index | photos_path | -|GET |/photos/new |new | new_photo_path | -|POST |/photos |create | photos_path | -|GET |/photos/1 |show | photo_path(id) | -|GET |/photos/1/edit |edit | edit_photo_path(id) | -|PUT |/photos/1 |update | photo_path(id) | -|DELETE |/photos/1 |destroy | photo_path(id) | +|_.HTTP Verb |_.Path |_.action |_.named helper | +|GET |/photos |index | photos_path | +|GET |/photos/new |new | new_photo_path | +|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) | +|DELETE |/photos/:id |destroy | photo_path(:id) | -NOTE: Use +photos_path+, +new_photos_path+, etc. to generate paths for this resource. +NOTE: Use +photos_path+, +new_photo_path+, etc. to generate paths for this resource. h4. Specifying Constraints @@ -672,14 +678,14 @@ resources :photos, :as => "images" will recognize incoming paths beginning with +/photos+ and route the requests to +PhotosController+, but use the value of the :as option to name the helpers. -|_.HTTP verb|_.Path |_.action |_.named helper | -|GET |/photos |index | images_path | -|GET |/photos/new |new | new_image_path | -|POST |/photos |create | images_path | -|GET |/photos/1 |show | image_path(id) | -|GET |/photos/1/edit |edit | edit_image_path(id) | -|PUT |/photos/1 |update | image_path(id) | -|DELETE |/photos/1 |destroy | image_path(id) | +|_.HTTP verb|_.Path |_.action |_.named helper | +|GET |/photos |index | images_path | +|GET |/photos/new |new | new_image_path | +|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) | +|DELETE |/photos/:id |destroy | image_path(:id) | h4. Overriding the +new+ and +edit+ Segments @@ -776,14 +782,14 @@ end Rails now creates routes to the +CategoriesController+. -|_.HTTP verb|_.Path |_.action |_.named helper | -|GET |/kategorien |index | categories_path | -|GET |/kategorien/neu |new | new_category_path | -|POST |/kategorien |create | categories_path | -|GET |/kategorien/1 |show | category_path(id) | -|GET |/kategorien/1/bearbeiten |edit | edit_category_path(id) | -|PUT |/kategorien/1 |update | category_path(id) | -|DELETE |/kategorien/1 |destroy | category_path(id) | +|_.HTTP verb|_.Path |_.action |_.named helper | +|GET |/kategorien |index | categories_path | +|GET |/kategorien/neu |new | new_category_path | +|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) | +|DELETE |/kategorien/:id |destroy | category_path(:id) | h4. Overriding the Singular Form @@ -823,10 +829,10 @@ If you want a complete list of all of the available routes in your application, For example, here's a small section of the +rake routes+ output for a RESTful route: <pre> - users GET /users {:controller=>"users", :action=>"index"} -formatted_users GET /users.:format {:controller=>"users", :action=>"index"} - POST /users {:controller=>"users", :action=>"create"} - POST /users.:format {:controller=>"users", :action=>"create"} + users GET /users(.:format) users#index + POST /users(.:format) users#create + new_user GET /users/new(.:format) users#new +edit_user GET /users/:id/edit(.:format) users#edit </pre> You may restrict the listing to the routes that map to a particular controller setting the +CONTROLLER+ environment variable: |