Page Content

Tutorials

Web Application Frameworks In Ruby: Power Of Ruby On Rails

Web Application Frameworks in Ruby

Modern web development requires web application frameworks like Ruby on Rails, which give developers the structure and automation they need to create intricate applications quickly. An open-source framework for building web-based applications in Ruby is called Ruby on Rails (Rails). It is commonly acknowledged as Ruby’s game-changing application, playing a major role in bringing Ruby out of relative obscurity outside of Japan.

You can also read Building Your First Website Sinatra In Ruby With Examples

Core Philosophy and Architecture

Two fundamental architectural ideas underpin Rails:

Model-View-Controller (MVC) Architecture: The MVC pattern is the foundation of Rails apps. This divides the application into three sections, each of which has a predictable name:

  • Model: The data set is represented by a collection of Ruby classes that typically correspond to database tables.
  • View: The user interface, which is housed within templates.
  • Controller: A Ruby class that defines the actions (operations) on the model through its methods.

Convention Over Configuration: With a merciless commitment to automating repetitive activities and enforcing sensible default behaviours, Rails operates. Compared to other frameworks, Rails has far less “paperwork” because so many parameters and names can be logically deduced from other information. Because of this commitment to tradition, developers may focus on finding solutions as soon as possible. Rails is also regarded as an especially useful tool for quickly creating prototypes.

Installation and Application Setup

Rails comes in the form of a Ruby package, or gem.

Installation: To install the rails gem and its dependencies, use the RubyGems command line tool:

$ gem install rails --include-dependencies

Application Generation: A WEBrick testing server and a unit testing framework are included in the initial directory structure that is created when you run the rails command and supply the name of your application (for example, “status”).

$ rails status
      create
      create  app/controllers
      create  app/helpers
      ... (generates full directory structure)

You can also read What Is Command Line Applications In Ruby With Examples

Key Components with Code Examples

After creating the application framework, the MVC components are built using the script/generate command line tool.

Controller and Actions (C in MVC)

As a subclass of ActionController::Base, a controller is a Ruby class. The end user’s actions are defined by its methods.

Code Example: Generating a Controller and Actions

The generate script generates the required action stub and controller methods:

$ ./script/generate controller user add delete login logout
# This command creates: app/controllers/user_controller.rb

Rails searches for a hello controller and runs the world action method inside it when a user accesses a URL such as http://www.example.com/hello/world. Instance variables that the view will require are set by the controller method.

View and Templates (V in MVC)

Included in .rhtml files, the view makes use of ERb templates. Ruby code can be blended with plain text, frequently HTML, with ERB (Embedded Ruby), a lightweight templating framework.

Template Syntax and Logic The value of a Ruby expression can be included into the template using the <%= %> directive, whereas <% %> runs Ruby code without producing any output.

Code Example: View Template (index.rhtml snippet)

A straightforward application that displays system status uses variables set by the controller (such as @time and @ps) that are accessed by the view:

<h1>Processes running at <%= @time %></h1>
<pre><%= @ps %></pre>

Layouts A layout template (often app/views/layouts/application.rhtml) that supplies the header and footer renders views. With the special directive <%= @content_for_layout %>, the view content is injected.

Model and Database Integration (M in MVC)

CRUD (create, read, update, and delete) applications that handle database data benefit greatly from Rails.

Object Relational Mapping (ORM) Rails uses the ActiveRecord framework to integrate a database. This package makes it possible to manipulate data without writing raw SQL by automatically defining Ruby classes that access database tables.

Code Example: Generating a Model (ActiveRecord Class)

You create the unique model class Person if you have a database table called people:

$ ./script/generate model Person
# This creates: app/models/person.rb

Data Manipulation You can use Ruby methods on the Person class (ActiveRecord code) to execute database actions once the model has been produced.

# Creates a new entry in the 'people' table without writing SQL
Person.create(:name => "John Doe", :email => "john@doe.com")

Conventions are handled by ActiveRecord, which expects the respective model classes to be singular (like Person) and table names to be pluralised noun phrases (like people).

Additional Web Service Capabilities

Rails is capable of exposing features as web services in addition to standard web interfaces. A built-in web service generator in Rails allows controller operations to be exposed via XML-RPC and SOAP. To expose Ruby methods to external systems, you must define type information for the method signature in the API file.

You can also read What Is RDoc In Ruby? The Official Ruby Documentation System

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index