Skip to content

Ruby on Rails - Framework

A web framework provides a structured way to build web applications, handling common tasks and allowing you to focus on your application’s unique features. Ruby on Rails is a prominent example of a framework that follows the Model-View-Controller (MVC) architectural pattern.

When building a Rails application, your development process generally involves these core activities:

  • Describe and Model Your Application’s Domain: This involves identifying the key entities in your application (e.g., users, products, articles), their attributes, and how they relate to each other. This translates into database table designs and corresponding Rails models.
  • Define Application Logic and Behavior: This is about specifying what users can do within your application. For example, users can create accounts, products can be added to a cart, articles can be published. This logic resides primarily in Rails controllers and models.
  • Design the User Interface: This is how users interact with your application. You’ll design web pages (views) to display information and forms for user input. This involves HTML, CSS, and JavaScript, often managed by Rails views and helpers.

Ruby on Rails elegantly manages these tasks using the MVC pattern.

The Model-View-Controller (MVC) principle separates an application into three interconnected components, each with a distinct responsibility:

The Model represents your application’s data and business logic. It’s responsible for:

  • Interacting with the database (querying, creating, updating, deleting records).
  • Enforcing data validation rules (e.g., an email must be in a valid format).
  • Managing relationships between different data entities (e.g., a user has many posts).
  • Encapsulating complex business logic related to the data.

In Rails, this is primarily handled by Active Record. Active Record is an Object-Relational Mapper (ORM) that maps database tables to Ruby classes and table rows to Ruby objects. This allows you to work with your data using idiomatic Ruby code instead of writing raw SQL queries.

The View is responsible for presenting data to the user. It’s what the user sees and interacts with in their web browser. Views in Rails:

  • Are typically HTML templates with embedded Ruby code (ERB files, e.g., index.html.erb).
  • Receive data from the Controller and render it dynamically.
  • Should contain minimal logic, focusing primarily on presentation.
  • Can also render other formats like JSON or XML for API responses.

Rails uses Action View to manage views. Action View provides template rendering, helpers for common UI tasks (like generating forms or links), and support for layouts to maintain a consistent look and feel.

The Controller acts as an intermediary between the Model and the View. It handles user requests, interacts with the Model to fetch or modify data, and then selects a View to present that data. Responsibilities include:

  • Receiving incoming HTTP requests from the browser.
  • Interpreting request parameters (e.g., form data, URL segments).
  • Invoking appropriate Model methods to perform business logic or data operations.
  • Preparing data to be passed to the View.
  • Rendering the appropriate View or redirecting the user to another URL.

Action Controller is the Rails component that manages controllers. It handles routing, request processing, session management, caching, and more.

A typical request-response cycle in a Rails MVC application looks like this:

  1. A user interacts with the application in their browser (e.g., clicks a link or submits a form), sending an HTTP request to the server.
  2. The Rails Router inspects the request URL and HTTP method to determine which Controller action should handle it.
  3. The designated Controller action is invoked. It might read parameters from the request.
  4. The Controller interacts with the Model(s) to fetch, create, update, or delete data. The Model communicates with the database.
  5. The Model returns data (if any) to the Controller.
  6. The Controller prepares any necessary data (often as instance variables like @users) and selects an appropriate View to render.
  7. The View template is rendered, incorporating the data provided by the Controller, to generate an HTML page (or other format like JSON).
  8. The web server sends the generated HTML page back to the user’s browser as the HTTP response.

This separation of concerns makes Rails applications easier to develop, test, and maintain.

While the core Rails framework components (Active Record, Action View, Action Controller, collectively known as Action Pack for the latter two) reside within the installed Rails gem, you primarily interact with them through your application’s directory structure:

  • app/models/: Contains your Active Record model classes.
  • app/views/: Contains your Action View template files, organized by controller.
  • app/controllers/: Contains your Action Controller classes.

Active Record provides the database interaction layer. Action Controller and Action View (part of Action Pack) handle request processing and presentation. Rails orchestrates these components to provide a cohesive development experience.