MVC Architechture
Understanding MVC Architecture
The Ruby On Rails Framework uses an MVC Architecture. It's a fundamental part of how the framework is structured and it is designed to help us not repeat ourselves. MVC is a fundamental aspect of rails, which is important to understand right from the start. M, V, and C. The 'M' stand for Model, the 'V' stands for View, and the 'C' stands for Controller. The Model refers to the data objects that we use. It's the object-oriented approach to design.
Many things can be objects in our models but the data in our Database will be the most common type of object that we'll put there. The View is the presentation layer. It's what the user sees and interacts with. The web pages, the HTML, the CSS, the JavaScript. The Controller processes and response to user events. Such as clicking on links and submitting forms. The Controller makes decisions based on the request and then controls what happens in response. It controls the interaction with our Models and our Views. Let's take a look at a couple of diagrams that I think will make this all clearer.
First, let's look at Basic Web Architecture, we've got a Browser that interacts with a web page. Now, of course, there's a web server sitting in between those, but this is a simplified view. This web page might have lots of code that make decisions and finally outputs something back to the Browser. If its Database enabled, the page can interact with the Database and pull information out of the Database, and then return that back to the Browser. But the code to do these things is in one page, a single, long, script. In contrast, the MVC Web Architecture breaks up that single page by function.
| MVC architecture |
The Browser communicates with the Controller, which contains only code involved in making decisions about what should happen based on the Browser request. Then, if we need to interact with the Database, we'll have the Controller talk to our Model. And in our Model, we'll put all the code related to our data, and on connecting to the Database, and then the Model will return its results back to the Controller. The Controller can go back to the Model if it needs, and the Model can go back to the Database, and so on. But finally, when the Controller decides that it's ready to return a result to the Browser it will send its results to the View, the presentation layer.
Which will contain the code related to what HTML, JavaScript, and CSS should be returned back to the Browser. Essentially, we've just taken that one page, that single web page, and broken it up based on its function, into the Controller, the Model, and the View. Rails is built using this MVC Architecture. You need to try and follow this architecture and keep your code in the right places. Decision Code should go in the Controller, Data Code should go in the Model, and Presentation Code should go in the View.
Rails actually has names for the code libraries it uses for these three parts. It calls the Controller "ActionController", and the View is "ActionView". The Model is called "ActiveRecord", not "ActionRecord" but "ActiveRecord". Those names are going to become familiar, as you continue working with Rails. Rails also packages together ActionController and ActionView as something called "ActionPack", so if you ever see the term "ActionPack" it's really just ActionController and ActionView being grouped together because the Controller and the View are very closely related.
Keep this MVC Architecture in mind as you continue to work. It's gonna help you to understand how Rails structures things, and where we should be putting your code.
CHEERS,
Maitrey Patel (maitrey684)
Comments
Post a Comment