Ruby on Rails Examples - Ruby on Rails

How to create a library system?

Let us see how to create a simple and operational online library system for holding and managing the books, which has a basic architecture. We are building it using two ActiveRecord models to describe the types of data that is stored

  • Books,which describes an actual listing.

  • Subject, which is used to group books together.

What are the steps for Creating Rails Applications?

  • Create the basic skeleton of the application using the rails command.

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

  • Configure the application to know where your database is located and the login credentials for it.

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

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

  • Write Controller Code to put a life in your application.

  • Create Views to present your data through User Interface.

Now let us see each step in detail:

How to create an Empty Rails Web Application?

We have already seen that Rails has a set of helper scripts that automate many of the things you do when developing a web application. Here, let us use one such helper script to create the entire directory structure and the initial set of files to start our Library System application.

Go into ruby installation directory to create your application and run the following command to create a basic skeleton for library application. It will create the directory structure in the current directory.

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.

Let us see how to use library/app subdirectories where most of our development work - creating and editing files will be done.

  • The controllers subdirectory :

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

  • The views subdirectory:

It holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.

  • The models subdirectory:

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

  • The helpers subdirectory:

It 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.

How to start Web Server?

We have already seen that Rails web application can run under virtually any web server, but the most convenient way to develop a Rails web application is to use the built-in WEBrick web server.

Now, 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.

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

This will start your WEBrick web server.

Next, open your browser and browse to http://127.0.0.1:3000.

If everything is goes fine, then it will generate a greeting message from WEBrick as follows. Otherwise check your settings again.

Web server welcome

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails Topics