Push Forward your Web Apps with Ruby on Railsby QArea Team on December 8, 2011
Ruby on Rails represents a recent entry into the Web application development world. It is gaining mindshare fast, even while in beta versions. Through automating the development of the most common types of Web application Rails succeeds while not straight jacketing you in case you wish to add atypical or custom requirements. Furthermore, if to compare to great number of Free Libraries which present individual aspects of a Web application, Rails consists of a properly integrated set of tools for all aspects. Recently Ruby on Rails is getting Web development by storm. The underlying technologies are following:
- Ruby is the simple, free, portable, extensible, straightforward, interpreted scripting language for easy-and-quick object-oriented programming, which like Perl, sports a lot of features to handle text files as well as perform system-management tasks.
- Rails represents an open source, full-stack Web framework in Ruby for creating real-world applications easily and with less code.
- A full-stack framework denotes that in Rails all layers are built to work together thus it is not necessary to repeat yourself and may deploy a single language from top to bottom. In Rails, everything is developed in Ruby. Rails runtime extensions and reflection over configuration annotations and files.
The first thing to understand about Rails is its MVC (model/view/controller) architecture. Meanwhile this approach is not special for Rails or even to Web applications because it opposed to other programs – Rails offers a focused and distinct MVC way of thinking. If you move aside from the MVC approach, Rails grows more unwholesome that in case you follow its paradigm.
Skeleton model app/models/contact.rb
class Contact < ActiveRecord::Base
Controllers, in its abstract form, make your app logic. Consequently, the Ruby scripts in an app/controllers/directory of app will upload model data into variables, save it back and influence it.
Rails views are placed the same place we leave Ruby code. Rails involves a good template language for .rhtml files which comprises of pure HTML with embedded Ruby code. The surface appearance of a Rails application screen is commonly governed by CSS style sheets. The .rhtml format represents an enhancement of HTML. Each year a simple HTML file is also valid RHTML template by itself, but there is not much point in overlooking the scripting control that RHTML renders you.
Rails provides the tools that are basically a set of code generators. Rails doesn’t get in your way, however it saves majority of the work of manual programming – or eases you into the parts which demand manual coding through providing first-class scaffolding “for free”.
Developing a simple application
There are several tutorials are available on the Ruby on Rails site which may help you by creating a not difficult Rails application. Here the sample application is like them; whereupon there is a particular way to get started right in creating a Rails application. The sample application presents a basic address book. It illustrates the common steps for creating an application:
- Generate the model
- Build the application
- Get Rails running
- Create content
Creating the AddressBook model
The first you should do for any application is to build a database for its data to live in. If to consider technically, this step doesn’t require to happen first, but it demands to happen early, this must be obvious to create the database before any app code, even automatically built code. Let’s build a database in MySQL and produce a first table in this database.
Generating a MySQL database and table
[~/Sites]$ cat AddressBook.sql
CREATE DATABASE IF NOT EXISTS AddressBook;
CREATE TABLE IF NOT EXISTS contacts (
id smallint(5) unsigned NOT NULL auto_increment,
name varchar(30) NOT NULL default '',
created_on timestamp(14) NOT NULL,
updated_on timestamp(14) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name_key (name)
) COMMENT='List of Contacts';
[~/Sites]$ cat AddressBook.sql | mysql
Creating the AddressBook application
After you have a database to interact with, build the AddressBook application. The first thing is just running rails to make the basic directories and scaffold code: Generating basic code and directories
[~/Sites]$ rails AddressBook
After you have created the AddressBook/ directory and needed children, it is necessary to perform just a hollow initial configuration. First of all, set the database through modifying a YAML configuration file in such a way:
Configure database access
[~/Sites]$ cd AddressBook
[~/Sites/AddressBook]$ head -6 config/database.yml # after editing
At the end, you need to serve the data. Rails includes its own single-function Web server, WEBrick, that is absolutely good for our experiment. Also you might follow instructions at the Web site of Ruby on Rails to configure Apache or other servers to serve Rails apps by means of FCGI or plain CGI.
Kicking over the WEBrick server
[~/Sites/AddressBook]$ ruby script/server -d
=> Rails application started on http://0.0.0.0:3000
[2005-03-21 17:57:38] INFO WEBrick 1.3.1
[2005-03-21 17:57:38] INFO ruby 1.8.2 (2004-12-25) [powerpc-darwin7.8.0]
Rails provides you with an extremely fast way to develop flexible Web applications; this article just hardly touched on what it’s like to work with Rails. The full framework includes many useful classes and methods for implementing the actions most deployed in Web-based applications. The great thing in Rails is that it fosters an entire “Rails way of thinking,” since it comes finished with all the supporting code you require. That is a big plus over other frameworks and toolkits that just provide raw materials to work with. Rails development gives you a clear path from a half-formed idea to an entirely functioning Web application.
Check out our related articles: