In Rails 2.1, one of the largest improvements made to the Rails framework was the addition of RESTful resources through ActionController::Resources. A RESTful resource is something that can be pointed at and will respond with a representation of the data requested. Instead of assigning the action to be taken within the parameters of the request or in the path, the method used along with the path determines what will happen.
For example, let’s take a product model and make it RESTful. To do this, we need to add the line into routes.rb:
By adding this line into the routes configuration, the Product model is now a RESTful resource.
Our routes now might look something like Table 1-3.
|GET||/products/1/edit||edit||edit_product_url(:id => 1)|
|PUT||/products/1||update||products_url(:id => 1)|
|GET||/products/1||show||product_url(:id => 1)|
|DELETE||/products/1||destroy||product_url(:id => 1)|
As you can see, the combination of the request method used and the URL will determine the action.
1.14.1. RESTful Resources
There are three different ways to add resources to your routes:
Allows resource to be accessed RESTfully:
map.resources :products, :users
Allows resources to be accessed RESTfully within a container controller:
map.resources :admin do |admin| admin.resources :users end
Resources that have customized options. Available options ...