DEV Community

Leela Khadka
Leela Khadka

Posted on

MVC - Rails

Image description
MVC Architecture

MVC stands for Model View Controller which is a pattern for the architecture of a software application. MVC separates an application into multiple components which makes it easy to understand and maintain.

To communicate with a Rails application a User will send a request via browser where browser acts a view and the rails application receives the request via routing engine and passes it to the specific controller handler method defined in the rails application. Here controller layer acts as a bridge between View and Model. From controller the requests are handled over to Model's where Model interacts with Database to fetch/update data and returns back to controller and controller then returns back the processed data to the User via View layer.

Model
In our rails application we use ActiveRecord as a dependency module which will handle the business logic and database communication like fetching/creating/updating/deleting (CRUD). This plays a role of Model in our MVC architecture.

For example in the below diagram there is a Student Model which can create Students and also perform all CRUD operations on Student Model using ActiveRecord.

Image description

Controller
The ApplicationController class acts as a controller of view and model where it takes in the request from the browser performs some business logic if needed and calls the Model to do the database operations and sends back the result to View so User can View the processed data.

Image description

View
In MVC architecture View should contain presentation logic only where we show something on the browser by using frontend frameworks like React. Basically View layer should be mostly used for showing/presenting data and does not directly interact with database and only communicates with controller layer of the backend application like Rails. View layer will contain Html code, css code and Java script code in case of our React application as shown below.

Image description

Top comments (0)