1.4. Building Your First Resources

Earlier, you saw the initial design for Soups OnLine where two resources, recipe and ingredient, were described. It's time to put them in your application, using the Rails generate script. The action for the script is scaffold. (In versions of Rails prior to 2.0, it was called scaffold_resource.) The syntax is simple: the singular name of the resource, followed by pairs of the form attribute:datatype for each attribute you want initially placed in the resource.

The data-type portion of each pair can be any type available for use as a data type in a Rails migration: binary, boolean, date, datetime, decimal, float, integer, string, text, time, and timestamp.

There's no expectation that you have to have the attribute list correct up front (it can always be changed), but it should just be an easy place to start. The commands and responses look like this (for clarity, I've removed lines where Rails shows that a directory already exists):

$ ruby script/generate scaffold recipe title:string servings:string 
description:string directions:string create app/views/recipes create app/views/recipes/index.html.erb
create app/views/recipes/show.html.erb create app/views/recipes/new.html.erb create app/views/recipes/edit.html.erb create app/views/layouts/recipes.html.erb create public/stylesheets/scaffold.css create app/models/recipe.rb create test/unit/recipe_test.rb ...

Get Professional Ruby on Rails™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.