1. Computing

Discuss in my forum

Ruby on Rails

An Internet Programming Language


Ruby on Rails, often simply referred to as Rails, is a web development framework based on the general purpose programming language Ruby. Rails was designed to streamline the tasks typically involved in programming for the web, that is, dynamically generating web pages, accessing databases, and retrieving information from the web server and the user's browser.

Rails makes use of the MVC (Model/View/Controller) software architecture pattern in organizing software development. MVC represents an attempt to separate a program into components whose functionality is similar in many applications. This makes it possible to re-use components, replace or modify components without impacting other components, and abstract the common functionality and form templates that speed up development of new applications.

The MVC design pattern is based on the observation that many application programs have a "model" component, which represents the collection of information that the application is dealing with, a "view" component, which represents the user interface, and a "controller" component, which implements the functions that retrieve or update the specific information requested by the user.

In simple applications, as in Rails' default configurations, the model is implemented as database tables. Such database tables represent a persistent storage of the information or data being modeled. Data modeling refers to the process of developing and implementing data structures that reflect the logical relationships between the data as best as possible. Rails encourages a naming convention that enables automatic implementation of models. For example, a model named Customer maps to a database table customer using a file customer.rb under the app/models directory.

In the case of Rails, the controller typically takes a user request as input and performs some action that generates and displays the information requested. Such requests are usually initiated through a web browser. In the simplest case, the controller determines which "view file" should be displayed. In Rails an action can be linked to an http request using the Rails router, which maps the request to an action/rule, properly passing the parameters to the corresponding procedure. If this mapping is implemented, external web requests will be directly routed to specified actions.

Ruby includes a special syntax and programming language, similar to PHP and JSP, that is designed for convenient dynamic generation of text documents, such as web pages. Such text documents are specified by writing templates in erb files, where erb stands for embedded Ruby. An erb template is basically a text file that may contain any number of embedded Ruby instructions, which are identified by special markers. These instructions can be used to insert any kind of dynamic content from databases or other sources.

The two most used tags are

<%= exression %>


<% scriptlet %>

The tag with the equal sign causes the interpreter to evaluate the expression and insert the result of the evaluation as string in place of the tag. For example, if @customer is a variable that whose value is "Jane Doe", the erb line

Dear <%= @customer %>

would output

Dear Jane Doe

to the document being generated.

The tag without the equal sign is used to mark so-called scriptlets, which are used to implement loops and conditionals. The following example generates a list in html format:

  <% for @title1 in @title_list %>
    <li><%= @title1 %></li>
  <% end %>

If the variable @title_list contains an array with the elements Thriller, Jailhouse Rock, and Yellow Submarine, the text produced by this code would be:

    <li>Jailhouse Rock</li>
    <li>Yellow Submarine</li>

Ruby on Rails relies on a web server and various JavaScript libraries for execution. For Ajax functionality is uses the libraries Prototype and Script.aculo.us. Rails also supports RESTful web services.

For more information see the official Ruby on Rails web site.

©2014 About.com. All rights reserved.