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

Ruby on Rails - Examples


Previous Page Next Page  

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 −

    Books, which describes an actual psting.

    Subject, which is used to group books together.

Workflow for Creating Rails Apppcations

A recommended work flow for creating Rails Apppcation is as follows −

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

    Create a database on the PostgreSQL server to hold your data.

    Configure the apppcation to know where your database is located and 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 Web 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 into ruby installation directory to create your apppcation.

    Run the following command to create a skeleton for pbrary apppcation. It will create the directory structure in the current directory.

tp> rails new 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.

Most of our development work will be creating and editing files in the pbrary/app subdirectories. Here s a quick run down of 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 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 under virtually any web server, but the most convenient way to develop 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.

tp> cd rubypbrary 
tp
ubypbrary> Rails server

It generates the auto code to start the server as shown below −

Rails Server

This will start your WEBrick web server.

Now open your browser and browse to http://127.0.0.1:3000. If everything is gone fine, then you should see a greeting message from WEBrick, otherwise there is something wrong with your setting. If everything goes well it will generate the output as follows.

Web Server

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 Rails Migration is and how it is used to maintain database tables.

Advertisements