Ruby on Rails

Simpler than J2EE


Tutorial Ruby is an object oriented scripting language. Rails is a Model-View-Controller (MVC) framework based on Ruby and used for developing web applications. An advantage of Ruby on Rails over J2EE is that it requires less code and doesn’t require any configuration files, except for a database configuration file. A Ruby on Rails web application may be developed simply with a web server and a database; and the Rails framework includes a built-in web server, WEBrick (another web server such as Apache may also be used). The Rails framework is configured for the MySQL database by default but Rails also supports PostgreSQL, SQL Server, Oracle and DB2 databases.

Overview of Ruby

Ruby is an interpreted scripting language for object-oriented programming: “interpreted” implies that a Ruby application is run without first compiling the application. Variables in Ruby do not have a type (a Ruby variable may contain data of any type). Variables in Ruby may be used without any variable declarations. Ruby, being an object oriented language, has features such as classes, inheritance and methods.

Overview of Rails

Rails is a web application and database persistence framework for developing web applications according to the MVC pattern. Models are used to model objects in a Rails application and are typically based on ActiveRecord. An ActiveRecord model class extends the ActiveRecord::Base class. Models provide object-relational mapping (ORM) between business objects and a database. Views are the user interfaces and are represented by RHTML or RXML templates, RHTML being Ruby embedded HTML, and RXML Ruby-generated XML. The controller is a class that extends the ActionController::Base class and consists of actions (methods). For each method in a controller class, you either define a view template with a matching name, redirect to a view template or a controller action or render text in the template that invoked the controller action with render :text method. A view template contains links to actions (methods) defined in the controller. A controller integrates the model with the view. The model models data objects, the controller defines business logic to process the data, and the view presents the data.

The model and controller scripts may be generated with Ruby on Rails commands, some of which are discussed in the table below:

Command (Variables are in italics) Description
rails applicationname Creates a Rails application of specified application name.
ruby script/server Starts WEBrick web server, which may be accessed at url http://localhost:3000
ruby script/generate model modelname Generates a model class, which extends ActiveRecord::Base class.
ruby script/generate controller controllername Generates a controller class, which extends the ApplicationController class.
ruby script/generate scaffold modelname controllername Generates a model class, which includes the scaffolding, and a controller class. controllername is optional in the scaffold generator command.
ruby script/generate migration migrationname Generates a ActiveRecord migration script.

Installing Ruby on Rails

In this section we shall install the Ruby on Rails framework, and RubyGems. RubyGems is the standard Ruby package manager. Download the Ruby Windows Installer application and double-click on the ruby184-19.exe application. Ruby Setup Wizard gets started in which click on Next and accept the license agreement. Select the default components to install, which include the RubyGems package manager. Specify a directory to install Ruby, c:/ruby being the default, and click on Install. Ruby and RubyGems gets installed. Next, install Rails. From the c:/ruby directory, the directory in which Ruby is installed, run the following command to install Rails and dependencies including activerecord and actionpack - Activerecord implements the model components of a Rails MVC application and actionpack implements the view and controller components:

c:/ruby>gem install rails --include-dependencies

Now install Oracle database 10g including sample schemas and create a database instance, ORCL for example. Next, install Ruby driver for Oracle database, which is required to connect to Oracle database from a Ruby on Rails application. Download this file (ruby-oci8-0.1.15-mswin32.rb) to c:/ruby directory and from the c:/ruby directory run the Ruby application for Oracle database drive:

 c:/ruby>ruby ruby-oci8-0.1.15-mswin32.rb

This installs the Ruby driver for the Oracle database. Next, we shall create a Rails application, rubyrails, using the following command:

 c:/ruby>rails rubyrails

This generates a Rails application directory structure. The root directory of the Rails application is rubyrails. The app sub-directory consists of sub-directories models, views and controllers for model scripts, view templates and controller scripts. The config directory consists of a database.yml configuration file, used to define a database configuration (as mentioned, by default this is configured for MySQL database). The db directory consists of a sub-directory migrate that consists of migration scripts.


Other stories you might like

  • Google Pixel 6, 6 Pro Android 12 smartphone launch marred by shopping cart crashes

    Chocolate Factory talks up Tensor mobile SoC, Titan M2 security ... for those who can get them

    Google held a virtual event on Tuesday to introduce its latest Android phones, the Pixel 6 and 6 Pro, which are based on a Google-designed Tensor system-on-a-chip (SoC).

    "We're getting the most out of leading edge hardware and software, and AI," said Rick Osterloh, SVP of devices and services at Google. "The brains of our new Pixel lineup is Google Tensor, a mobile system on a chip that we designed specifically around our ambient computing vision and Google's work in AI."

    This latest Tensor SoC has dual Arm Cortex-X1 CPU cores running at 2.8GHz to handle application threads that need a lot of oomph, two Cortex-A76 cores at 2.25GHz for more modest workloads, and four 1.8GHz workhorse Cortex-A55 cores for lighter, less-energy-intensive tasks.

    Continue reading
  • BlackMatter ransomware gang will target agriculture for its next harvest – Uncle Sam

    What was that about hackable tractors?

    The US CISA cybersecurity agency has warned that the Darkside ransomware gang, aka BlackMatter, has been targeting American food and agriculture businesses – and urges security pros to be on the lookout for indicators of compromise.

    Well known in Western infosec circles for causing the shutdown of the US Colonial Pipeline, Darkside's apparent rebranding as BlackMatter after promising to go away for good in the wake of the pipeline hack hasn't slowed their criminal extortion down at all.

    "Ransomware attacks against critical infrastructure entities could directly affect consumer access to critical infrastructure services; therefore, CISA, the FBI, and NSA urge all organizations, including critical infrastructure organizations, to implement the recommendations listed in the Mitigations section of this joint advisory," said the agencies in an alert published on the CISA website.

    Continue reading
  • It's heeere: Node.js 17 is out – but not for production use, says dev team

    EcmaScript 6 modules will not stop growing use of Node, claims chair of Technical Steering Committee

    Node.js 17 is out, loaded with OpenSSL 3 and other new features, but it is not intended for use in production – and the promotion for Node.js 16 to an LTS release, expected soon, may be more important to most developers.

    The release cycle is based on six-monthly major versions, with only the even numbers becoming LTS (long term support) editions. The rule is that a new even-numbered release becomes LTS six months later. All releases get six months of support. This means that Node.js 17 is primarily for testing and experimentation, but also that Node.js 16 (released in April) is about to become LTS. New features in 16 included version 9.0 of the V8 JavaScript engine and prebuilt Apple silicon binaries.

    "We put together the LTS release process almost five years ago, it works quite well in that we're balancing [the fact] that some people want the latest, others prefer to have things be stable… when we go LTS," Red Hat's Michael Dawson, chair of the Node.js Technical Steering Committee, told The Register.

    Continue reading

Biting the hand that feeds IT © 1998–2021