Hackernoon logoHow to Build an MVC App On Rails by@sajjad-ahmad

How to Build an MVC App On Rails

MVC is an acronym for Model View Controller. It's a design pattern for web application development. It separates the core components so that they work independently and change in one does not affect the other. The view is used to present data fetched by the controller from the controller. After getting a response with data from the Model it passes it to the View, which makes data presentable. It finally gives a response to the browser with success if the request is fulfilled or with an error. It's the main pillar of MVC architecture.
image
Sajjad Ahmad Hacker Noon profile picture

Sajjad Ahmad

This post will help new Rails developers to understand Modern Application development flow by teaching them MVC.

  1. Introduction
  2. Request and Response Concept
  3. MVC building blocks
  4. Model
  5. View
  6. Controller
  7. Advantages
  8. Disadvantages

MVC is an acronym for Model View Controller. It's a design pattern for web application development. Ruby on Rails uses this pattern by convention. It separates the core components so that they work independently and change in one does not affect the other.

See this picture to understand how the information flows inside a web developed using MVC pattern:

image

HTTP is a protocol that is widely used in client-server environments. It's based on request-response. The client or browser is the one who makes a request to the server and gets a response.

It means that the browser takes the first step in communication between client and server. This is the time now that we start our ride of MVC on Rails.

Rails app provides routes, a type of path for HTTP requests to land in application safely. See this for detailed info about Rails Routes.

Browser request hits controller directly, and it's controller role to pass this request to Model for database or to view to get a view template. Controller when gets a response gives the browser back that response which shows that it received information for the user.

Let's go through each component of MVC one by one!

Model

Model is the M in MVC. Model is used to interact with the database in your application. In Rails, it uses Active Record to access the database. The model can create validations and associations between models. See Rails Guide on Model.

View

The view is used to present data fetched by the controller from the Model. There are formats to present that data: PDF, HTML, JSON, etc. But most of the time web pages use HTML and CSS for styling. See Rails Guide on View.

Controller

Controller by name shows what it's responsible for. It's the main pillar of MVC architecture. It gets a request from the browser, passes it to the Model if it needs any data. After getting a response with data from the Model it passes it to View, which makes data presentable. It finally gives a response to the browser with success if the request is fulfilled or with an error. See Rails Guide on Controller

Advantages and disadvantages of MVC architecture

Advantages of MVC architecture:

  • Development of the application becomes fast.
  • Easy for multiple developers to collaborate and work together.
  • Easier to Update the application because every part of the application is separate.
  • Easier to Debug each part of an application.

Disadvantages of MVC architecture:

  • It is a bit hard to understand the MVC architecture.
  • Must have strict rules on methods.

There are not many disadvantages of MVC, so it's useful to look at advantages only!

That's it! Thanks for reading and supporting me!

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.