Developing a Web application based on database has never been this easy. Ruby
on Rails is a solution that speeds up the development process by providing a
skeletal code framework for application development. It is an Open Source
project written in Ruby, while Ruby itself is a pure object oriented programming
language. Rails provides a framework for developing web-based database driven
application. An application developed with Ruby on Rails is based on
Model-View-Controller (MVC) architecture. Faster application development is
possible as Rails follows two principles, viz, 'Don't Repeat Yourself' (DRY) and
'Convention over Configuration' (CoC). In this article, we will explore how Ruby
on Rails can be used for faster Web-app development and seeing these principles
in action.
|
Setup and configuration
We have carried the Ruby installer on this month's PCQ Professional CD. It
comes with the RubyGems package manager. You can install the Rails framework
either by installing it from within Ruby by calling a remote installation
command or by installing the InstantRails package. To install Rails, we used
RubyGems package manager. From the command prompt, we entered the following
command:
gem install rails --remote
During installation, RubyGems downloads all the libraries that Rails depends
upon and for each dependency it prompts before downloading. So press 'Y' (Yes)
for each of the dependencies. The other method is to extract the InstantRails
package and use the InstantRails.exe to start up with the development.
Categories |
||
Field name |
Attribute | Constraint |
Id | Int | Primary key |
Name |
Varchar(50) |
|
Description |
Varchar(250) |
|
Books |
||
Field name |
Attribute |
Constraint |
Id | Int |
Primary key |
Category_id |
Int |
Foreign Key to Categories |
Name |
Varchar(50) |
|
Author |
Varchar(50) |
|
Summary |
Varchar(250) |
App development
We will build an application based on MySQL database. The application will
be for books management in a library, whereby each book will be placed under a
specific category. So lets create a database 'pcqrubydemo' and under that create
two tables-Categories and Books.
As Rails is an application development framework, it creates the basic
structure needed for the application. Another convention that Rails follows is
that the name of the application and that of database should be same, so the
application name will be 'pcqrubydemo'. For Rails to create the structure we
will first make a directory called Rails Root Directory. From the command
prompt, we will navigate to this directory and give following command:
Rails pcqrubydemo
Here, a subdirectory 'pcqrubydemo' is created, which contains the complete
directory tree of folders and files for the application. Before moving on to the
application development, let's just look at the subdirectories that were created
for the application. As Rails is based on Model-View-Controller paradigm, it
uses the same to organize the source code.
* app/controllers: Controllers handles Web request from a user. All
controller classes are placed in this subdirectory.
* app/views: It holds HTML/JSP pages which are used for data input and
information display to user.
* app/models: This holds classes that model and wrap the data for
application's database.
Once you've created your database, you need to run the Scaffold command in Ruby on Rails to generate a framework for each table |
Now, as our database and empty application are ready, we will start building
a framework for the application based on the database. For this we need to edit
the database configuration settings of the application. First, we need to change
the database name, username, password and host in the file 'database.yml'
located in the config directory under the main application directory, ie, 'pcqrubydemo'.
After setting up the database configuration, we can instruct Rails to build up
the framework based on the database tables. As Rails is based on Convention over
Configuration principle, i.e., by establishing a standard naming convention the
framework will develop the relevant code. This means the classes and methods
will be generated by the framework based on the table and column names. These
generated code structures are called 'scaffolding', and the term suggests, it
provides the outer support to the application. So to generate the scaffolding
for the Categories table, we will use the following command:
ruby script\generate scaffold category category
When you install Rails through remote install call, the dependencies are automatically downloaded |
This will instruct Rails to construct the scaffolding for the application, ie,
the code structure related to the Categories table. The convention that Rails
follows is of singular model class name mapping to plural database table names.
In our case, it is category mapping to Categories table and book mapping to
Books table. As per the MVC architecture, the code generated will contain the
model, view and controllers files. Rails names the model file as category.rb and
places that under app/models folder. Same way, the controllers file,
category_controller.rb, is generated and placed under app/controllers
subdirectory of the application root folder. The HTML pages for showing insert
and delete of the database entries are also created under app/views folder.
Similarly, we will also create the scaffolding of the application based on books
table by following command:
ruby script\generate scaffold book book
This will generate the model and controller classes for the books table and
also generate the views regarding the table. Now, we have the application ready
to work on.
The listing of different categories and the page showing the category selection for the new book creation page |
Testing the application
To see how Rails has developed the application, we first need to start the
built-in webserver WEBrick. To start it, we use following command from the
application's root directory 'pcqrubydemo':
ruby script/server
In the browser window, browse to http://localhost:3000/category. This will
open a page from where we can view, create, edit and insert a category into the
database. Similarly, if you suffix the same URL with book, it will set the page
to display the books list. Both pages show links to create, read, update and
delete entries from both the database tables. Rails has created the relevant
code structure for these functions and eliminated the work a developer puts in
to code for those functions. For entering a new book, we do not have option to
choose the category for that book. Now to put the category selection option, we
just need to modify model definition for category and book. In category.rb, we
will define the relationship that exists between Categories and Books table.
class Category < ActiveRecord::Base
has_many :books
end
As just one category can be assigned to a book, we need to modify the class
Book. We will add following code that will ensure this.
class Book < ActiveRecord::Base
belongs_to :category
end
To give the selection option to the user, we'll modify some Rails generated
view code related to Book class. Since Rails is based on 'Don't Repeat Yourself'
principle, we have a partial form created by Rails that corresponds to both New
and Edit methods. We just need to modify the partial form and the changes get
reflected on New and Edit views of the Book class. The following code snippet
will show the category selection option on the New book creation page:
To view the category selection option, first restart the WEBrick server. When
we browse to new Book page we view the Category Selection option there. Now to
make the pages more appealing, we can modify the HTML tags and put the retrieved
data into a table format.
Conclusion
As you can see, Ruby on Rails can be very helpful for creating Web
applications that are database driven, and the best thing is that it requires
little coding only.