The Scaffold

Rails is supposed to be a rapid application development environment, but so far we just have a back-end database. To get some instant front-end delight, we'll use another Rails feature called scaffolding.

Scaffolding is a monstrously fast and terribly tempting short-cut to create an interface for a model. It can be used to near-instantly (literally) generate some boiler plate pages for performing CRUD (Create, Retrieve, Update, Delete) operations on a database table (via a model). The code is basic and crude, and the interface is ugly as sin; but with minuscule effort, the scaffold enables you to knock together a simple administrative back-end for a database table within seconds.

The scaffold is also a useful learning tool, as it demonstrates the minimum amount of code you need to write in your own controllers. It is also useful in situations where you might be the only person who ever administers the database: if it doesn't need to be fancy, the scaffold plus a few tweaks is a great way to quickly create the administrator views.

To generate a scaffold, you simply need to specify the name of the model you want to scaffold for. In our case, the model is called Person. Therefore, from inside RAILS_ROOT, we would run:

$ ruby script/generate scaffold Person

This produces quite a few files, including controller classes and view templates for all the CRUD actions on that model. Using this generated interface, we can now add records to the people table in our database.

To do this, we need to start our application:

$ ruby script/server

Then browse to http://localhost:3000/people to see the application in its full glory:

The Scaffold

I warned you it would be ugly. But the point is, we now have a working administrative interface, which (with a bit of spit and polish) Rory could show to his colleagues at Acme.

Feel free to play around, but bear in mind we are in the development database, and will likely be destroying all the data at some point by running migrations backwards and forwards.

Note

Alternatives to the basic scaffold

If the scaffold is simply too ugly for you to look at, you could try one of the prettier (but more complicated) alternatives:

The Ajax Scaffold Generator (http://ajaxscaffold.com/) provides virtually the same functionality as the default scaffold, but wraps it in a more responsive interface. It also has enhanced facilities for editing records in one table which are associated with another table (see the section Associations Between Models).

Streamlined (http://streamlined.relevancellc.com/) is a framework for generating an administrative back-end for a set of Rails model classes. The resulting interface is very rich in functionality and much smoother to use than the scaffold. Streamlined also provides a declarative language for specifying the layout of the administrative interface, how relationships between models are displayed in the interface, plus an authentication framework. On the negative side, the documentation is practically non-existent, and it may well be difficult to figure out how to configure the generated code.

We'll go into scaffolding in more detail in the next chapter. Through the rest of this chapter, we'll see how to build up the models associated with our new database tables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.223