O'Reilly logo

Professional Ruby on Rails™ by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required