Rails Introduction and Model-View-Controller (MVC)


In the next few sections, you will learn the basics of rails programming through implementing a simple project. To be prepared, we highly recommend that you first complete the "Getting Started" tutorial on the official Rails website. Unfortunately, it is a bit lengthy as it will take you a few hours to get through. However, it's very well-written and will give you a solid understanding of Rails basics if you read everything carefully.

Below are some notes and topics that may be especially useful while you're completing this tutorial:

  • rails routes is an extremely useful command. Use it frequently to see what URI routes correspond to which controller actions that you've defined. You'll learn a lot about "Rails magic" if you understand all of the routes in your application, many of which Rails has automatically set up when you declare resources in your routes.rb.

Model View Controller

Before you dive into the tutorial, it would be helpful for you to understand more about the MVC philosophy of organizing web applications. Consider that you have an application in which every view corresponded to a single resource, like a user or a post or something else. Your only operations are such that you can create more of that resource, update it, or delete it. The functions of this application would be pretty limited. Nowadays, applications need to support much much more, allowing users to edit the same resource in several different contexts, allowing the same resource to be represented is several different media, and maybe even allowing several different clients to utilize the same API. The result is that you need a very structured, flexible, and extensible framework of organizing all of your data and services, and thus the philosophy of MVC was born.

Here is a breakdown of the components of MVC:

  • Model: A wrapper around your application data. The model may have light helper functions around the actual data itself. However, the model should never need to know how it needs to be represented in the view.
  • View: Displays data to the user (GUI), and also accepts user input from buttons and other actions and sends those requests to the controller.
  • Controller: Interfaces between the view and the model to create, read, update, and delete the various resources in your application.

Pictorially, this is an example of how a request propagates through the Rails MVC:

  1. A user sends an HTTP request from their browser
  2. The Router (discussed later) interprets the request and selects the appropriate controller action (index)
  3. The Users controller's index method calls User.all to get all users from the model
  4. The User model retrieves all users from the database
  5. The users are sent back to the controller
  6. The controller stores all users in the @users instance variable, which is made available in the index.html.erb view
  7. The view processes the data supplied in order to generate an HTML page that the browser can read
  8. The HTML is sent back to your browser to be displayed

That was an awful lot to digest, but don't worry! The specifics of this will be made a lot more clear when you start working on the various components yourself.

Assignment

Again this assignment is optional but highly recommended, as it gives you a very nice cross-sectional look into Rails development. If you do this assignment, then you should be able to skim over the next couple of Rails guides more quickly as they will review concepts that you will have implemented.

Make sure that you are in the ruby_rails directory that you acquired from the previous lab. Also make sure that you are on the master branch.

cd ruby_rails
git checkout master

Then create a new branch to hold your work for this assignment.

git checkout -b rails_intro

Complete the first tutorial on the official Rails website up to and including section 8 (don't do Security or anything following, as we'll use Devise). If you've already done this tutorial in the past, feel free to proceed to the next section to submit your previous work. If you don't happen to have your files anymore, contact Ken or Tony.

How to Submit

When you are ready to submit, make sure that you are on the rails_intro branch, and then perform the following commands.

git status  # check that everything is ok
git add -A
git commit -m "Your commit message."
git push origin rails_intro

results matching ""

    No results matching ""