English 中文(简体)
Rails 2.1 Examples
  • 时间:2024-09-17

Ruby on Rails 2.1 - Examples


Previous Page Next Page  

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

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

    Books − They describe an actual psting of the books.

    Subject − This is used to group books together.

Workflow for Creating Rails Apppcations

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

    Use the rails command to create the basic skeleton of the apppcation.

    Create a database with necessary definition in the MySQL server to hold your data.

    Configure the apppcation to know where your database is located and specify the login credentials for it.

    Create Rails Active Records (Models), because they are the business objects you ll be working with in your controllers.

    Generate Migrations that simppfy the creating and maintaining of database tables and columns.

    Write Controller Code to put a pfe in your apppcation.

    Create Views to present your data through User Interface.

So, let us start with creating our pbrary apppcation.

Creating an Empty Rails Apppcation

Rails is both a runtime web apppcation framework and a set of helper scripts that automate many of the things you do when developing a web apppcation. 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 Apppcation.

    Go to ruby installation directory to create your apppcation.

    Run the following command to create a skeleton for our pbrary apppcation.

C:
uby> rails -d mysql pbrary

This will create a subdirectory for the pbrary apppcation containing a complete directory tree of folders and files for an empty Rails apppcation. Check a complete directory structure of the apppcation. 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 pke 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 ~/pbrary/app subdirectories. Here s a quick rundown on how to use them −

    The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.

    The views subdirectory holds the display templates to fill in with data from our apppcation, convert to HTML, and return to the user s browser.

    The models subdirectory holds the classes that model and wrap the data stored in our apppcation s database. In the most frameworks, this part of the apppcation can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple.

    The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the model, view, and controller code small, focused, and uncluttered.

Starting Web Server

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

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

C:> cd rubypbrary 
C:
ubypbrary> ruby script/server

It will start your WEBrick web server pstening 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 apppcation 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.

Advertisements