Flowing through MVC
At first the MVC might seem somewhat confusing and intimidating, but everyone can learn it. This is how I view the MVC. First, what does MVC stand for? Models, Views, and Controller. By connecting the model, controller and views, they can work together in order to help us create a Web App. The model consists of classes that can perform CRUD (Create, Read, Update, Delete) functions. The Controller takes in a request, evaluates, and gives back a response. The View shows a visual presentation of information and logic in the web browser. Let’s take a quick look of how the MVC flows.
Web Browser and URL Request — Inside the web browser, we type in a URL and will take us to the site. What is typed is actually a request and the server receiving it will interpret it.
Server Router and Router File — When the server receives it. It will look inside of the route file of the web application. Inside of this file, there will be specified information called routes. Each route consists of a few columns, the first one being the HTTP verb. The HTTP will instruct on whether the request wants to retrieve or give data from our app. The second will be the URL path request. That is the URL we would type to go to that specific page. The third column will consist of the controller and action within that controller. It will look for the controller named before the “#” and afterward the action. After the request gets matched to a route, we will proceed to the controller. (Also, using resources, will help shorten your routes.
Controller — The controller contains all the actions that we want to access. It receives a request and then gives back a response based on the action performed.
Actions — The action contains the logic to be performed when receiving a request. The action connects to our view files. If the name of the action and file are the same, Rails will pair them up naturally. Otherwise, you will need to “render” the file inside the action.
Models and Database — The Model is responsible for CRUD (create, read, update and delete) actions by connecting to the database. Once the model delivers or retrieves data from the database, it will go back to the action.
Views — After the model connects information to the action, the action will proceed to the view file. The view will be a representation of what we want the user to see in the web browser. Inside the view file is where all the html code will be contained. After it compiles the code, wit will send back a response.
Response to the Web Browser — The response is what we see when the MVC has finished working together and done its job. It will display all the information asked for on your web browser.
I hope this helped to understand the way that the MVC structure works. It really shows that it was created with detail and logic. MVC really helps with the organization, structure and refactoring of code we type.