---
# System prepended metadata

title: MVC Architecture In Ruby on Rails
tags: [Ruby on Rails]

---

# MVC Architecture In Ruby on Rails
###### tags: `Ruby on Rails`
## Introduction
![MVC Figure](https://i.imgur.com/U3niz9w.png)
Unlike PHP or some other framworks, most of framworks including Rails are designed based on MCV architecture pattern. MVC stands for Model, View and Controller respectively. In this framwork design style, the project work can be divides into small pieces and easier to assign jobs without conflict and interruption between each other when the size of team gains. Furthermore, different Rails developers can follow the convention of MVC and make the structure of projects identical such as controller should be in ```app/controllers``` and model should be in ```app/models```.

Let's talk about the MVC with a example, imagine you are surfing on Youtube and you want to watch a video.

1. When you type in the URL of Youtube, the first part you will meet is routes, which will tell where to go, for example the homepage of Youtube. Every distinct page represents a unique route just like every home address is unique.

2. The routes will tell the controller what action is going to be done. That is if I search NBA the Youtube will list all the videos about NBA. Here, I go to another route(searching NBA) and routes tells controller to do the "index" action(list all video), and if I click one of the video, the controller does another "show" action to show the user the video. If routes can't find the action that user assign in controller, then it will return 404.

3. There are usually more than one actions in a controller, those actions are actually methods in Ruby. When the routes find the specific action, action will tell website what to do next.

4. After we know what to do, the model will tell the database what data is needed. In this case, we want all the videos about NBA, the model tells database to list all videos about NBA. Model is not database itself, instead it more like a interpretator between controller and database. It turn all human action into SQL for database to read and get the data we want. 

5. After we get what we want from database, the model will hand over the data to controller. And the next part is about view, view will layout the data and present to the user just like the Youtube page is comfortable to read because view makes it more prettier than the data used to be.

MVC divides the project into small pieces, layout goes to view, the process go to controller and the logic goes to model. In this case, the project team can eaisly assign works and developers can work with less conflict.



