Lesson 2 -- Databases
In writing software for any platform, we need to be able to do 3 things:
1. get input from the user
2. store the information
3. output results to the user
We have , on the Rails platform, learnt how to output text to the user
via a dynamic web page. In this chapter we deal with databases. In the
next chapter, we will see how to get input from the user, and you will be
ready to start writing your application.
Imagine we wish to create a database for books, and wish to track their
names, numbers and authors. In Rails, we simply, on the command line,
go to the root folder of the application, and type the following
script/generate scaffold book name:string num:integer author:string
now, assuming the server is still running from the previous chapter,
fire up your browser and go to http://localhost:3000/books/ and you
will be pleasantly surprised to find that you not only have a
functioning database, but also a minimal application that allows you
to add edit and destroy records in this database. Click on "new book"
to come to http://localhost:3000/books/new where you see a regular
data entry screen for the name, number and author of the book you wish
to enter into the database.
How do we use the database in our own programming? Next chapter. For now,
let me leave you with the output of the above commands, to help you
diagnose the fault if things go wrong.
>script/generate scaffold book name:string num:integer author:string
route map.resources :books
db/development.sqlite3 already exists
== 20090511140924 CreateBooks: migrating ======================================
== 20090511140924 CreateBooks: migrated (0.2250s) =============================
Again, just 3 lines of typing!