Model View Controller Pattern

Codecabulary Home / Learn Rails / Model View Controller Pattern

The Model View Controller separates the concerns of an application into its three core functions; imagine a dating site:

  1. The dating site is powered by tons of user information. Users create profiles, list their interests, dislikes, and answer silly questions that are probably irrelevant to dating. This information needs to be stored somewhere. The model deals with information storage and retrieval.

  2. The dating site lets its users see their own profile and others' profiles. Maybe it has a page that shows you a random person a la Russian Roulette. Each of those pages has its own look and feel. The view deals with presentation of content like a newspaper layout.

  3. A whole lot of logic goes into the site's recommendations for you. Using some complex algorithm (like throwing darts) the site presents a list of users it thinks you'd be compatible with. The controller handles the program's logic, including the importance of information in relation to other data.

Since most applications have these three basic needs, developers tend to separate these concerns into their own homes in the program. By convention (and by default), the app folder generated by a rails new command contains a subfolder for controllers, a subfolder for models, and a subfolder for views.

MVC illustrates an overarching concept in programming: convention over configuration, the principle that common structural concerns should follow a common structural patterns, the way sentences abide grammatical rules (and when they don't, you end up with Finnegan's Wake).

The Flow of Information in the MVC Pattern in Rails

The MVC pattern has the cobenefit of making it much easier to learn Rails (or Django or whatever) because each piece of the program can be learned about separately. Below, let's take a look at how all the pieces interact:

Illustration of the MVC Pattern in Rails, courtesy of the [Hartl Tutorial](http://ruby.railstutorial.org/chapters/a-demo-app#sec-mvc_in_action).

  1. The user makes a web request, in this case, they've typed datingsite.com/users into the address bar. For whatever reason, let's assume that's the url of the page that contains the users datingsite.com thinks you are compatible with.

  2. The Rails router (config/routes.rb file by convention) matches the request against a pattern, like a telephone operator would find the right line to connect you to (does anyone still get that reference?). The router routes the request to the appropriate action in the userscontroller to decide what to do with the request (remember, the controller handles the logic). We see in the example that the router sends the index message to the userscontroller, instructing it to handle the request using its index action.

  3. In order to perform all of its fancy controller logic (it needs to find the names and pictures of the users it thinks your compatible with to show you), the controller requests that the model send back the right users from the database and all their information.

  4. The model queries the database for the users. The model, like the controller, is just plain Ruby code, but because it inherits from the powerful ActiveRecord class, Base, the model can translate this plain Ruby into the right SQL request for us. The database is happy to oblige a speaker of its native tongue, and sends back the users to the model.

  5. Yet again, the model obediently returns the correct users to the controller.

  6. The controller finishes its logic and hands off the correct users to the view, along with whatever other information might need to be passed off.

  7. The view contains placeholders for the users handed off by the controller, and it builds the right HTML to send back to the user. It hands off the HTML to the controller.

  8. The controller sends the complete HTML back to the browser, and in a few seconds from the time of the request, the user discovers that their mother was right, and that all the good men really are taken once you turn thirty.