Introduction to Advantages of Rails
Rails are the vehicle that brings Ruby to the Web. Following are some advantages of rails:
- Rails are used to create an application framework.
- Rails generate controller.
- Via Rails, we can create an action by adding a method to a controller.
- Rails provide a way to create a view template.
- Add a second action to a web application.
- Render any template.
- Link to actions from views.
1 Common Advantages
- Rails is a web application framework that uses Ruby as its programming language
- Rails provide application skeleton and add certain configurations such that the configurations from scratch are not required.
- It makes the basic CRUD operations easy, the high amount of code as we used to do in frameworks like servlets and struts etc. is not so messy while working with Ruby and Rails, rails also eliminate the need of XML file configurations
2 Rails Unique and Advantageous MVC Design Pattern
- The core of the Rails framework carries the MVC design pattern.
- ActiveRecord and the model classes that you build on top of ActiveRecord provide the model layer of your application, this layer provides object-relational mapping (ORM) between rails classes and the database you use.
- The view layer of a Rails application is implemented in ERB template files, ERB files are a mixture of HTML and embedded ruby code and are just like the JSP files.
- Rails support two built-in files :
- RXML file – Way to create an XML file using ruby code
- The rails controller implementation is also a part of Action Pack library, rails keep the methods of interacting with client and server sides simple.
a)Rails Directory and Contents
Rails applications have a common way in terms of directory structure and location of files. The directories and their respective contents are as follows :
- app: Where all the application’s MVC code goes
- config: Application configuration files
- DB: Database schema and migration files
- doc: Documentation for your application
- lib: Application-specific custom code that isn’t part of your MVC code
- log: The application log files automatically created by Rails
- script: Rails scripts for code generation, debugging, and performance utilities
- test: Unit-test related code and related files
- tmp: Cache, session information, and socket files used by the Web server
- vendor: Where Rails plug-ins are installed
b) Rails Scripts:
- Rails Console: The Rails Console is a command-line utility that lets you run a Rails application in a full Rails environment right from the command-line. This tool comes handy while debugging the developed code
- WEBrick: WEBRick is the web server included with rails framework and helps in testing at the level of localhost.
- Generators: Rails have incorporated generation scripts, used in the automatic generation of model classes and controllers automatically for the application.
- Migrations: Used to define the structure of a database, prevents writing SQL to create a database, each change in the database schema results in separate migration file.
3 Advantages of creating an Application with Rails
There are certain steps to be followed as mentioned below –
- Project creation by using rails command
- Database configuration
- Model, Service and Controller creation
- Application Styling
Step 1: Project Creation
Let’s say you are creating a project named as “Phonebook”, you can create a directory in which you want to manage all the code on rails, then type given script for project creation “rails phonebook”
Once you do this, you will find that rails have created the structure/skeleton for you, the exemplary structure is shown below
You can find that controllers, models, and views have been creating, placing the MVC design pattern inline and in addition to this rails created logs for production, development and testing arenas too.
- WEBrick server creation
Type the command “ ruby script/server Webrick”, this makes rails to start the server and the server’s name is passed as an argument, at last, to tell ruby that this server has to be created among the available choices.
Default port bound to WEBrick is 3000, any changes if desired in this context can be done at environment variables.
Step 2: Database set-up
Let’s use sqLite3 for instance and tell it to create a table for us
sqlite> CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
PhoneNumber TEXT NOT NULL,
Step 3: MVC creation
Use the given script to generate the model first “ruby script/generate model Phonebook”. This will create files for you in the given directories-
app/models/phonebook.rb // this represents your model object
- Controller and views creation
Type the given command to create controller & views “ ruby script/generate controller phonebook” It creates the following files :
Step 4: Execute the app and check for working
Create a template first in the app/views/phonebook directory and let’s call that file “index.html.erb”, type some text into this file like “Hey! It works” and then save it.
Start the server by given script “ruby script/server”
The console will narrate the story and when the application runs, go to URL http://localhost:3000/contact/index here index refers to the action you would have created, where the results fetched from a database will be passed and thereby rendered by the view.
This was the basic introduction carrying advantages of rails framework, you can explore each part to its depth and there are some explorable advanced features also as mentioned below :
- RESTful development with rails
- Using ActionMailer with rails for sending emails to subscribers.
- ActiveResource ( client side compliment of REST) and XML ( for data translation)
- Deployment made easy with Capistrano
- Adding plug-ins.
- Using prototype and rails
This has been a guide to the advantages of Rails. Here we have discussed the advantages of using Rails in the real world. You can also look at the following article to learn more –