When developing a Rails app, the request/response cycle is a useful guide to trace how a user's request flows through the app. A common category of web apps that are built with Ruby on Rails are form-based apps. These kinds of apps give users a form for submitting data, stores that data in a database, and lets users view or modify those entries. Here's how a user's request flows through a form-based app.

Request/response cycle

Turn 1

In the first turn of the request/response cycle, the page with the form is displayed to the user.

  1. A user opens his browser, types in a URL, and presses Enter.
  2. When the user presses Enter, the browser sends a GET request for that URL.
  3. The GET request hits the Rails router (config/routes.rb). The router maps the URL to the correct controller action to handle the request.
  4. The action receives the GET request and passes it on to the view.
  5. The view renders the page as HTML.
  6. The controller sends the HTML back to the browser. The page loads and the user sees the page with the form.

Turn 2

When a user submits the form, it triggers the second turn of the request/response cycle, where the submitted data is saved into the database.

  1. The user fills in the form and submits the form.
  2. When the user submits the form, the browser sends a POST request to the Rails app.
  3. The POST request hits the Rails router. The router maps the POST request to the correct controller action.
  4. The action receives the POST request. The action retrieves the submitted data from the form, and uses the model to store the data into the database.
  5. The model completes its task.
  6. The controller action passes the request on to the view.
  7. The view renders the page as HTML.
  8. The controller sends the HTML back to the browser. The page loads and the user sees it.