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.