Keeping Track: A Simple Guestbook
Most applications will need to do more with data—typically, at least, they’ll store the data and present it back as appropriate. It’s time to extend this simple application so that it keeps track of who has stopped by, as well as greeting them. This requires using models. (The complete application is available in ch04/guestbook002.)
Warning
As the next chapter on scaffolding will make clear, in most application development you will likely want to create your models by letting Rails create a scaffold, since Rails won’t let you create a scaffold after a model with the same name already exists. Nonetheless, understanding the more manual approach will make it much easier to work on your applications in the long run.
Connecting to a Database Through a Model
Keeping track of visitors will mean setting up and using a database.
In Heroku and Instant Rails, the databases should set up at the
beginning, requiring no further intervention. Otherwise, in Rails
2.0.2 and later, this should be easy when you’re in development mode,
as Rails now defaults to SQLite, which doesn’t require explicit
configuration. (Earlier versions of Rails required setting up MySQL,
which does require configuration.) To test whether SQLite is installed on your system, try issuing the
command sqlite3 -help from the
command line. If it’s there, you’ll get a help message. If not, you’ll
get an error, and you’ll need to install SQLite.
Once the database engine is functioning, it’s time to create ...