Creating a View
After creating a model and a controller, then its time for us to create a view. We can use two approaches to build views for our Rails application. One is to make use of scaffolding; the other is to “go it alone.” We’ll look at scaffolding very briefly, but we won’t be using it much in the development of our Shovell application. I’ll introduce just enough to give you a taste
of this topic, then leave it up to you to decide whether or not you find it worthwhile in your own projects.
Generating Views with Scaffolding
In the early days of Rails, scaffolding was one of the features that the Rails community used as a selling point when promoting the framework. Ironically, this feature also received a considerable amount of criticism, though this was
largely due to critics failing to understand fully the intended uses of scaffolding.
Scaffolding is a tool that quickly creates a web interface for interacting with your model data. The interface lists the existing data in a table, and provides an easy way to add new records and manipulate or delete existing ones.
There are two approaches to using scaffolding:
Temporary scaffolding
Temporary scaffolding, which is simply a one-line addition to one of your controllers, is a technique for generating code that’s not intended to be built upon. Much like its construction namesake, temporary scaffolding is intended to support a primary structure, not form part of it. Once the primary structure has been completed, the scaffolding is taken away.
Permanent scaffolding
Permanent scaffolding, as you might have guessed, is intended to be extended, modified, and built upon. It uses the generate script which we’ve experimented with in this chapter to create fully working template code that can be modified over time. Features provided by the template code can then be tweaked or implemented in a different manner, and code that doesn’t suit your project can be removed.
Regardless of which approach you use, you should keep in mind that scaffolding is a tool designed for quick interaction with models, and should only be used as such. It is by no means intended to be a fully automated tool for generating web
applications (or even administration interfaces).