From 7305ef842b675bf965f063de681a96294577fb84 Mon Sep 17 00:00:00 2001 From: Trevor Turk Date: Mon, 18 Mar 2013 06:35:19 -0500 Subject: The HTTP method helpers are preferred over match and should be explained before named routes --- actionpack/lib/action_dispatch/routing.rb | 44 +++++++++++-------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing.rb b/actionpack/lib/action_dispatch/routing.rb index 8881417583..550c7d0e7b 100644 --- a/actionpack/lib/action_dispatch/routing.rb +++ b/actionpack/lib/action_dispatch/routing.rb @@ -69,6 +69,22 @@ module ActionDispatch # Routing::Mapper::Scoping#namespace, and # Routing::Mapper::Scoping#scope. # + # == Non-resourceful routes + # + # For routes that don't fit the resources mold, you can use the HTTP helper + # methods get, post, patch, put and delete. + # + # get 'post/:id' => 'posts#show' + # post 'post/:id' => 'posts#create_comment' + # + # If your route needs to respond to more than one HTTP method (or all methods) then using the + # :via option on match is preferable. + # + # match 'post/:id' => 'posts#show', via: [:get, :post] + # + # Now, if you POST to /posts/:id, it will route to the create_comment action. A GET on the same + # URL will route to the show action. + # # == Named routes # # Routes can be named by passing an :as option, @@ -157,34 +173,6 @@ module ActionDispatch # Encoding regular expression modifiers are silently ignored. The # match will always use the default encoding or ASCII. # - # == HTTP Methods - # - # Using the :via option when specifying a route allows you to - # restrict it to a specific HTTP method. Possible values are :post, - # :get, :patch, :put, :delete and - # :any. If your route needs to respond to more than one method you - # can use an array, e.g. [ :get, :post ]. The default value is - # :any which means that the route will respond to any of the HTTP - # methods. - # - # match 'post/:id' => 'posts#show', via: :get - # match 'post/:id' => 'posts#create_comment', via: :post - # - # Now, if you POST to /posts/:id, it will route to the create_comment action. A GET on the same - # URL will route to the show action. - # - # === HTTP helper methods - # - # An alternative method of specifying which HTTP method a route should respond to is to use the helper - # methods get, post, patch, put and delete. - # - # get 'post/:id' => 'posts#show' - # post 'post/:id' => 'posts#create_comment' - # - # This syntax is less verbose and the intention is more apparent to someone else reading your code, - # however if your route needs to respond to more than one HTTP method (or all methods) then using the - # :via option on match is preferable. - # # == External redirects # # You can redirect any path to another path using the redirect helper in your router: -- cgit v1.2.3