MVC Application in Ruby on Rails
MVC is a pattern for the architecture of a software application. It separates an application into the following components:
1. Models for handling data and business logic.
2. Views for handling graphical user interface objects and presentation
3. Controllers for handling the user interface and application
MVC on Rails
Rails promote the concept that models, views, and controllers should be kept separate by storing the code for each element as separate files in separate directories. This separation continues within the code that comprises the framework itself. The classes that form the core functionality of Rails reside within the following modules
ActiveRecord is the module for handling business logic and database communication. It plays the role of the model in our MVC architecture. Pattern one that this component implements in order to perform its role in the MVC world.
ActionController is the component that handles browser requests and facilitates communication between the model and the view. Controllers will inherit from this class. It forms part of the ActionPack library.
ActionView is the component that handles the presentation of pages returned to the client. Views inherit from this class, which is also part of the ActionPack library.
ActiveRecord ships with database adapters to connect to SQLite, MySQL, and PostgreSQL. A large number of adapters are available for other popular database server packages, such as Oracle, MongoDB, and Microsoft SQL Server, via RubyGems.
Database abstraction is a way of coding an application so that it isn’t dependent upon anyone’s database. A code that’s specific to a particular database server is hidden safely in ActiveRecord and invoked as needed. The result is that a Rails application is not bound to any specific database server software should you need to change the underlying database server at a later time, no changes to your application code are required.
Tables are the containers within a relational database that store our data in a structured manner, and they’re made up of rows and columns. The rows map to individual objects, and the columns map to the attributes of those objects. The collection of all the tables in a database, and the relationships between those tables, is called the database schema
The ActionPack Library
ActionPack is the name of the library that contains the view and controller parts of the MVC architecture. Unlike the ActiveRecord module, these modules are more intuitively named. ActionController and ActionView.ActionController (the Controller)
The controller handles the application logic of the program, acting as the glue between the application’s data, the presentation layer, and the web browser. In this role, a controller performs a number of tasks including
Deciding how to handle a particular request (for example, whether to render a full page or just one part of it) Retrieving data from the model to be passed to the view Gathering information from a browser request and using it to create or update data in the model
ActionView (the View)
One of the principles of MVC is that a view should contain presentation logic only. This principle holds that the code in a view should only perform actions that relate to displaying pages in the application; none of the code in a view should perform any complicated application logic, nor store or retrieve any data from the database. In Rails, everything that is sent to the web browser is handled by a view