Ruby on Rails 2.1 Examples - Ruby on Rails 2.1

How to create a sample Rails application ?

This chapter will talk about how we create an application using Rails framework using simple and efficient examples. For example we will create something simple but operational online library system for holding and managing the books.

One must be patient enough to learn as it is a bit tough going through next few chapters but not difficult.

Online library system application (Example) has a basic architecture and will be built using two ActiveRecord models to explain the types of data that is stored in your database

  • Books − Describe an actual listing of the books.
  • Subject – To set books together.

How to Create Rails applications?

Below is the suggested workflow for creating Rails applications

  • Rails command will be used to create the basic skeleton of the application.
  • Create database with required definition in the MySQL server to hold your data.
  • Configure the application to know where the database is located and create the login credentials for database.
  • Create Rails Active Records (Models) which are the business objects will be working with Active controllers.
  • Create Migrations, which makes database tables and columns creating and maintaining is simple.
  • Create Views and controllers to present your data in a specified manner through User Interface.

How to create an empty Rails application

Rails works as a runtime web application framework and also as a helper script, which automates a lot of things when user developing a web application.

Here, let’s know how to use Rails helper script to create the directory structure and the primary set of files to start our Library System Application.

  • Start ruby installation directory to create the Library System application.
  • To create a skeleton for Library application run the below command.

Using this command can create a subdirectory for the library application, which contains a whole directory tree of folders and files for an empty Rails application.

Let’s use MySQL database here –d mysql. User can use other databases like oracle or postgressusing -d option. Rails uses SQLite database as default database.

Below describes how user can create and edit the files in the~/library/app subdirectories. In Rails major part of the app development includes in creating and editing subdirectories.

  • Controller handles a web request from the user, for this the “controllers subdirectory” will be created.
  • Views subdirectory holds the display templates, which will be filled in with data from users application and converts into HTML then return to the user's browser.
  • Models subdirectory holds the classes that model and wrap the data stored in our application's database. In the most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple.
  • Helpers subdirectory holds and allows helper classes to assist the model, view, and controller classes. Subdirectory will also help to maintain the model, view, and controller code to be precise, focused and in order.

Starting Web Server

WEBrick is the default web server which comes with Rails framework. But Rails web application will run under virtually any web server. Using the built-in WEBrick web server to build and test a Rails web application is easy way.

WEBrick server will start from the app directory as follows, which runs on port number 3000.

Once the above command is entered, WEBrick web server starts to listen for Web Requests at port number 3000 at local machine.

User should open browser and browse to http://127.0.0.1:3000 and should see a greeting message from WEBrick web server.

Below is the screen for a successful setup

screen for a successful setup

If user cannot see a greeting message from web server as shown in the screen, check to see the whole setup and should be fixed if any errors while setting up before proceeding further.

What is next?

The next session would include the topics on how user can create databases for their application and how access the created databases, and then we learn about what is Rail Migration and its use in maintain the DB tables.


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

Ruby on Rails 2.1 Topics