Datasheet
Chapter 1: Building Resources
8
def self.down
drop_table :recipes
end
end
The t.string syntax is a Rails 2.0 method for spelling what would previously have been written
t.column :string . The timestamps method adds the special Rails columns created_at and
updated_at . The creation of the ingredient resource generates a similar migration at db/migrate/002_
create_ingredients.rb
.
Routes
The most important additions are the new routes added to the routes.rb file, which are the source of
all the RESTful magic. As created by your two generators, the routes look like this:
map.resources :ingredients
map.resources :recipes
Standard Routes
The purpose of the routes.rb file is to control the conversion from an HTTP request to a Rails method
call. Each of these
map.resources lines causes Rails to associate URLs that start with the resource name
to the resource for that controller, in this case
/recipes would invoke the recipe controller. So far, it
sounds similar to a traditional Rails route in
:controller/:action/:id format. The difference is that
the REST routes infer the action to call in the controller based on the HTTP method invoked. There are
seven standard actions in a REST controller. The following table shows the standard interpretation of
URLs and the HTTP methods that are used to describe the corresponding controller actions. Each
controller action also has a path method, to be called inside views for
link_to and form actions, as well
as a URL method, which is called inside the controller when you need to redirect to a different action.
(continued)
URL Called HTTP Method
Controller
Action Path Method URL Method
/recipes/1 GET show recipe_path(1) recipe_url(1)
/recipes/1 PUT
update recipe_path(1) recipe_url(1)
/recipes/1 DELETE
destroy recipe_path(1) recipe_url(1)
/recipes GET
index recipes_path recipes_url
/recipes POST
create recipes_path recipes_path
/recipes/new GET
new new_recipe_path new_recipe_url
/recipes/1/edit GET
edit edit_recipe_
path(1)
edit_recipe_
url(1)
c01.indd 8c01.indd 8 1/30/08 4:02:22 PM1/30/08 4:02:22 PM