Anatomy of a Request

This page reviews the details when a user initiates a request to the server (e.g. clicking on a link, pressing a button). It is useful to distinguish between requests that just read from the database (uses GET) and requests that make changes (write, update, destroy) to the database (uses POST).

When reviewing the steps for each type of request, it may be helpful to reference the web architecture diagram:

MVC Diagram

Here's the PDF version and Visio version of the MVC diagram.

Request that reads from the database

Usually these are requests that are initiated when a user clicks on a link.

Properties:

Process

  1. Rails produces the link (e.g. /items/23) using thelink_to method. Example: link_to "Item", item_path(@item)
  2. The user clicks on a link
  3. The browser uses the GET method to request the URL of the link. Example: /items/23
  4. The server's routing system parses the URL, identifying any parameters in the URL (e.g. ID = 23). The parameters are placed in the params table.
  5. The routing system determines the controller (e.g. Items Controller) and calls the method that corresponds to the URL (e.g. show).
  6. The controller may obtain parameters from the params table. Example: to get the value of the id parameter: params[:id])
  7. The controller obtains values from the data model, performs additional calculations and assigns results to instance variables, which will then be displayed in the view. Example: @item = Item.find(params[:id])
  8. Unless the controller methods calls the redirect_to method, the view matching the name of the method is automatically instantiated. Example: show.html.erb

Request by submitting a form

These are requests that are initiated when a user fills out a form and presses the submit button.

Properties:

Process

  1. Rails produces the form using form helper commands. Often the form is used to assign or modify properties of data objects. Example: to assign or change values to the @item variable, call form_for(@item)
  2. The user fills out the form and presses the submit button
  3. If the form is object-centered, the form packages the the object's properties and values into one parameter using the name of the object. Parameters are sent using the POST method and are not part of the URL.
  4. The server receives the posted parameters and places them in params table. For example, if the object has values for item.title and item.year, here is how parameters can be retrieved:
  5. The routing system determines the controller (e.g. Items Controller) and calls the method that corresponds to the URL (e.g. show).
  6. The controller may obtain object parameters from the params table. It may use this information to add to or update the model for the database.
  7. If the database save is successful, the controller methods typically redirects to an information page. If not successful, the controller typically renders (calls the view for) the form again.