Model-View-Controller Architecture of Ruby on Rails Framework

Rails relies heavily on MVC architecture. As such, Rails Framework has three marts: Model, View, and Controller.

The Model part of  MVC Architecture is representation of data in Rails. Model is defined as sub-classes of ActiveRecord::Base class. Each model in Rails framework maps to a single table which our application is configured to access. It is trivial to create a new model with Rails Framework. In order to accomplish this, you need to issue “rails generate model persons” command line.

This command will create a file under app/models  folder structure and name it persons.rb. Inside this file, Rails will define a Person ActiveRecord::Base class as well as add unit test for this model. Important to note that this command does not actually create tables in the database.

The Controller part of MVC architecture is a Ruby subclass of ActionController::Base. Methods of this subclass help define operations on the model. In other work each operation is defined as method in the controller.

Controller generation can be done via call to rails: “rails generate controller” and name of the controller being passed along. Here is an example of complete command line to generate person controller

“rails generate controller person add delete login logout”

The Rails command above will provision file called “person_controller.rb” under the “app/controllers/” folder” In addition, it will add stabs for four methods passed right after person controller: add, delete, login, and logout. In addition, similarly to Model creation, Controller creation will provision unit tests for this controller.

Views is a UI of MVC architecture. It relies on ERb for creating templates and storing it as .html.erb files. Each action of the controller has a corresponding .html.erb file as well so that UI can be created for each Controller action.

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…