Developing web applications using Ruby on Rails frame work
Ruby has been popularly known as object oriented general-purpose programming and scripting language since 1995. Released in the year 1995, Ruby is being used in many sectors including artificial intelligence (AI) and Machine learning programs. Written in a simple syntax, Ruby is easy to create, maintain, and edit.
Advantages of Ruby on Rails
Appreciable reduction in the code lengths and volume that enhances the speed of the project completion is seen with Ruby. Lessening the debugging pressure of the programmer, work moves at a smoother rate and more relaxed. Independent threading facility, irrespective of the operating system and the highly portable nature enhances the user-friendly nature of Ruby. Its built in web server WEBrick ameliorates Ruby’s performance in web applications and its excellent potentiality in loading the extension libraries dynamically with less maintenance. Ruby creates an environment that makes the programmers to work more clearly, faster and be more meticulous and highly imaginative.
Rails – a full-fledged framework that makes use of every facility and benefits of Ruby in database backed web applications. The highly time consuming portion for a developer is the writing of reusable packages of codes that are often called during the main application – frame works. Querying for logical results is the main task for a web programmer who is backed by a database. Database access is made more efficient and simpler with the help of most efficient database access libraries in Rails, called the Active records.
Rails use WEBrick by default, but they do use Lighttpd or Mongrel when installed. Within a short period of time, Ruby on rails has grown extensively as the most reliable alternative for a web development framework, making the task simpler and with more fun. This makes us to remember the words of the creator of Ruby, David Heinemeier Hansson, - "There's a booming commercial ecosystem growing up around Rails”.
RUBY for web development
Within a short period Ruby on Rails has created a remarkable impact in the presence of various web application frame works. The credit goes to the conversion of tiresome job of a developer into a work that’s more simple and fun to program with. Rails - being an MVC web framework; amazingly integrates models, views and controllers to lighten the configuration task.
Developers can enjoy the freedom of using the language with Ruby. Hence we have the existing configuration also written in Ruby. The simple conventions and self-managing reflections also contribute in reducing the configuration burden. The speed and accuracy in this framework increases customer satisfaction; above all he also enjoys the spirit of real time projects.
On the technical hand, Ruby can handle models (the classes representing RDBMS tables) with the help of Active records, with the help of which the program identifies the table of RDBMS that is being in use. The View or the display logic is implemented by the Embedded Ruby (.rhtml files) and the Controller implementations through Rails' Action Pack. WEBrick being used as a web server for production, more professional servers like Apache, Cherokee, Nginx or Lighttpd are also being in use. Rails maintain database neutrality; that enhances the feature of “any RDBMS or SQLite library can be used with rails”.
Setting up the environment
To develop a web application in Rails, install the following:
- Ruby
- The Rails framework
- Web server such as Apache 1.3.x or 2.x, nginx, lighttpd, IIS, or any FastCGI (or SCGI)-capable webserver with a mod_rewrite-like module.
- FastCGI or mod ruby for production performance.
- Database and driver (e.g. MySQL, PostgreSQL, or SQLite).
Here is a small program written on Rails platform. This program manages news databases and allows reading the news at the front end of the program. News can be stored at the RDBMS, modified and deleted. The news stored in the database can be viewed one by one clicking at the news number at the front end.
To test the application in system follow the instruction steps below.
Step 1: Open the command prompt and navigate to the folder to which the application should be created.
Step 2:Run the command:
Rails example, this will create a sub folder named 'example' and a set of folders and files for an empty Rails application.
Following is the sample output of the command.
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
Fig: 1- Creating an empty rails application
Step 3: After this open the folder 'example' and give the command
ruby script\server
this starts the server
Fig: 2- Starting the server
That’s it!!!... the application's index page can be run in http://127.0.0.1:3000
Sample Output
fig: 3- Sample Home Page created by Rails.
Now start with the app/controllers/application.rb file.
The default content of this file is
# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
end
Now need to create the home page of the site.
Create a controller app/controllers/home_controller.rb for the home page and add the following lines
class HomeController < ApplicationController
def index
end
end
Create a file, /app/views/home/index.rhtml and give some content, for e.g.-
<h1>Welcome</h1>
Fig: 4- Home Page of the application
Route the application to this file by Opening the file routes.rb in the ‘config’ folder. Uncomment and change the line
# map.connect '', :controller => "welcome"
to
# map.connect '', :controller => "home"
we can also give
# map.connect '', :controller => "home", :action=> 'index'
but can be skipped as it is default. The default file public/index.rhtml should be removed in this case.
Refresh the browser.
Connecting to the database
Edit /config/database.yml
The default database name given in this file is example_development. Create a database of this name and a table with following structure
CREATE TABLE IF NOT EXISTS news (
id int(11) unsigned ,
title varchar(255) ,
links varchar(255)
);
Open an additional command prompt and navigate to the application folder. Now issue the command
ruby script/generate model news
The sample output is as follows
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/news.rb
create test/unit/news_test.rb
create test/fixtures/news.yml
create db/migrate
create db/migrate/001_create_news.rb
Fig: 5- Creating model for news
Now a Model has been created.
Here run the following command in the command prompt
Ruby script/generate scaffold news
This is the sample output of the command
exists app/controllers/
exists app/helpers/
create app/views/news
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/news.rb
identical test/unit/news_test.rb
identical test/fixtures/news.yml
create app/views/news/_form.rhtml
create app/views/news/list.rhtml
create app/views/news/show.rhtml
create app/views/news/new.rhtml
create app/views/news/edit.rhtml
create app/controllers/news_controller.rb
create test/functional/news_controller_test.rb
create app/helpers/news_helper.rb
create app/views/layouts/news.rhtml
create public/stylesheets/scaffold.css
Now the files for the functions - create, list, show, edit and destroy news has to be created.
This can be done with a single command.
Ruby script/generate scaffold news
Fig: 6- Scaffolding
Done!!! see the code work at http://127.0.0.1:3000/news/
Fig: 7- Sample news control panel
Open the file /app/controllers/home_controller.rb and the change the content to
class HomeController < ApplicationController
def index
@news_pages, @news = paginate :news, :per_page => 1
end
end
Now news will be displayed in the home page by editing /app/views/home/index.rhtml
<span id="front-page-news"><strong>News</strong></span>
<br>
<table width="500" cellpadding="3" cellspacing="0" border="0" style="border: 1px #CCCCCC solid">
<tr ><td>
<% for news in @news %>
<% for column in News.content_columns %>
<%=h news.send(column.name) %>
<% end %>
<% end %>
</td>
<td align="right">
<%=pagination_links @news_pages %>
</td>
</tr>
</table>
See the result in http://127.0.0.1:3000/
Fig: 8- Result
Here news is the object, which holds the records in the table. The send() functions prints the news and the news links. pagination_links prints the page numbers of the paginated instance of news, @news_pages.
Try this sample and explore the new world of Ruby On Rails at your will and wish. Your whims and fantasies can be made real here.
For a better work and exposure follow the links below read them and do a lot
- Agile Web Development with Rails: A Pragmatic Guide (Pragmatic Programmers)
- Ruby on Rails: Up and Running
- Ruby for Rails: Ruby Techniques for Rails Developers
- Beginning Ruby on Rails (Wrox Beginning Guides)
- Beginning Ruby on Rails E-Commerce: From Novice to Professional (Rails)
- Build Your Own Ruby on Rails Web Application
- Ruby on Rails For Dummies (For Dummies (Computer/Tech))
- Ruby on Rails Power!: The Comprehensive Guide (Comprehensive Guides)
- Hacking with Ruby: Ruby and Rails for the Real World
- Beginning Ruby on Rails: From Novice to Professional (Beginning: from Novice to Professional)
- Rails Solutions: Ruby on Rails Made Easy (Solutions)
- Agile Web Development with Rails

