Ruby on Rails 2.1 - Examples


Subsequent chapters are based on the example taken in this chapter. In this chapter, we will create a simple but operational online library system for holding and managing the books.

This application has a basic architecture and will be built using two ActiveRecord models to describe the types of data that is stored in your database −

Workflow for Creating Rails Applications

A recommended workflow for creating a Rails Application is as follows −

So, let us start with creating our library application.

Creating an Empty Rails Application

Rails is both a runtime web application framework and a set of helper scripts that automate many of the things you do when developing a web application. In this step, we will use one such helper script to create the entire directory structure and the initial set of files to start our Library System Application.

C:\ruby> rails -d mysql library

This will create a subdirectory for the library application containing a complete directory tree of folders and files for an empty Rails application. Check a complete directory structure of the application. Check Rails Directory Structure for more detail.

Here, we are using -d mysql option to specify our interest to use MySQL database. We can specify any other database name like oracle or postgress using -d option. By default, Rails uses SQLite database.

Most of our development work will be creating and editing files in the ~/library/app subdirectories. Here's a quick rundown on how to use them −

Starting Web Server

Rails web application can run virtually under any web server, but the most convenient way to develop and test a Rails web application is to use the built-in WEBrick web server. Let's start this web server and then browse to our empty library application.

This server will be started from the application directory as follows. It runs on port number 3000 −

C:\> cd ruby\library 
C:\ruby\library\> ruby script/server

It will start your WEBrick web server listening for Web Requests at port number 3000 at local machine.

Now open your browser and browse to http://127.0.0.1:3000. If everything goes fine, then you should see a greeting message from WEBrick. Following is the screen for a successful setup −

Rails Welcome Message

If you do not get a greeting message as above, it means there is something wrong with your setup and you need to fix it before proceeding ahead.

What is Next?

The next chapter explains how to create databases for your application and what is the configuration required to access these created databases.

Further, we will see what is Rail Migration and how it is used to maintain database tables.