MVC, Routing, Controllers, Parameters, and the Full Request-Response Flow
Learn how an HTTP request moves through routes, controllers, models, and views in a typical Rails application.
Inside this chapter
- Understanding MVC in Rails
- Routes Connect URLs to Controllers
- Controller Example
- Request Lifecycle Thinking
Series navigation
Study the chapters in order for the clearest path from Rails beginner concepts to advanced production architecture. Use the previous and next links at the bottom of each page to move through the full tutorial series.
Understanding MVC in Rails
Rails is built around the Model View Controller pattern. Models handle business data and persistence rules, views render output for users, and controllers coordinate incoming requests, invoke domain logic, and choose what response to return. While real applications may add service objects or policies, MVC is still the foundational mental model.
Routes Connect URLs to Controllers
Rails.application.routes.draw do
resources :books
end
This single declaration generates standard CRUD routes such as index, show, new, create, edit, update, and destroy. Rails routing is powerful because it balances convention with flexibility.
Controller Example
class BooksController < ApplicationController
def index
@books = Book.all
end
def show
@book = Book.find(params[:id])
end
end
Controllers receive request parameters, prepare data, and render templates or JSON. Beginners should not overload controllers with heavy business logic. Controllers should coordinate, not become a dumping ground.
Request Lifecycle Thinking
A browser sends an HTTP request. Rails routing resolves a matching route. The controller action runs. Models fetch or change data. A view or serializer builds the response. Middleware, sessions, cookies, authentication, and error handling may participate along the way. Production-grade Rails work becomes much easier once this full request flow is clear.