Recipe 2.2. Jump-Starting Development with Scaffolding


Problem

You've got a good idea for a new project and have a basic database designed. You want to get a basic Rails application up and running quickly.

Solution

Once you have created your database and configured Rails to communicate with it, you can have Rails generate what it calls scaffolding. Scaffolding consists of the basics of a CRUD (create, read, update, and delete) web application, including controller and view code that interact with your model. When you generate scaffolding, you are left with a fully functional, albeit basic, web application that can serve as a starting point for continued development.

There are two ways to generate scaffolding in Rails. The first is to have Rails dynamically generate all the view and controller code needed to get your application running behind the scenes. You do this using the scaffold method of Action Controller. The second is to use the Rails scaffolding generator to create the scaffolding code in your application directory.

To demonstrate how scaffolding works, let's create a Rails application that lets you store a list of programming languages along with their descriptions. Start by setting up your database. Generate a database migration script with:

$ ruby script/generate migration build_db             

Doing so creates a file called 001_build_db.rb in your application's db/migrate directory. Open that file and add to it the following:

db/migrate/001_build_db.rb:

class BuildDb < ActiveRecord::Migration   def self.up     create_table :languages, :force => true do |t|       t.column :name, :string        t.column :description, :string      end   end   def self.down     drop_table :languages   end end

Run this migration script to build the languages table in your database:

$ rake db:migrate             

Once your database has been created and your Rails application is set up to connect to it, there are two ways to create scaffolding. The first is to use the scaffold method. Create a model named language.rb:

$ ruby script/generate model language             

Now create a controller named language_controller.rb:

$ ruby script/generate controller language             

These two generators show you what new files have been added to your Rails application. Open the newly created language controller and add the following call to the scaffold method:

app/controllers/language_controller.rb:

class LanguageController < ApplicationController   scaffold :languages end

Here, you are passing the scaffold method a symbol representing your model; :languages in this case. This single call tells Rails to generate all of the code needed to let you perform CRUD operations on the languages table.

To see the result, start up your web server:

$ ruby script/server             

and point your web browser at http://localhost:3000/language.

The second way to use Rails scaffolding is with the scaffold generator. If you choose to generate scaffolding using the generator, you don't need to create a model or controller explicitly, as with the previous technique. Once you have your database setup and configured, simply run the following from your application's root:

$ ruby script/generate scaffold language             

This command generates a number of physical files within your application directory, including model, controller, and a number of view files. The results of this scaffolding technique, as seen from your browser, are identical to the previous usage. You are left with a basic, functioning web application from which you can continue to develop and grow your application.

Discussion

Many people are initially lured into trying Rails after seeing videos of impressively quick code generation. For others, the idea of code being automatically generated by a framework feels invasive and may instead be a deterrent.

Before you make any decisions about Rails based on scaffolding, you should understand what code is created for you and how, and generally how scaffolding is used in real-world Rails development.

Most experienced Rails developers consider scaffolding merely a helpful starting point. Once they've created scaffolding, they generate the majority of the application manually. For developers new to Rails, scaffolding can be an indispensable learning tool, especially when the scaffolding code is created using the generator technique. The code created contains plenty of Rails code that demonstrates usage of the most common areas of the framework.

Figure 2-1 shows some screenshots of the kind of interface that's created by scaffolding.

Figure 2-1. CRUD scaffolding generated by Rails


A simple way to dress up the defaults is to modify the default stylesheet, but as you can see, without modifications, the design of these pages is probably not suited for much more than backend administration.

See Also

  • Section 2.11"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net