How Rails Processes a Request

In an effort to start writing and learning more, I started a list of things to write articles about. One of the items on my list was how Rails processes a request. After research, I am writing a blog post instead of an article, because there is not much code to write about.

railties\lib\dispatcher.rb:
Dispatcher.dispatch method:
The dispatch method passes the request to Routes.recognize, which returns an instance of the controller:
controller = ActionController::Routing::Routes.recognize(request)

On the controller, the dispatch method calls:
controller.process(request, response).out(output)

The process method calls perform_action, which calls the action method for the controller and renders the template, returning the ActionController::CgiResponse object. The out method on the response actually sends the output.

In actuality, more happens per request than this. In actionpack\action_controller.rb, 18 modules get included in ActionController::Base. Some of those modules modify the process or perform_action methods to add more logic into each request (e.g. calling filters before/after the action). However, the code turned out to be simple and easy to follow.

Mixins have frustrated me before trying to determine what is happening in Ruby code. Understanding everything that is happening can require looking at every single mixin. However, looking at this code really demonstrates how powerful mixins are. Following how a request is processed was extremely simple because I was only concerned with the basics and could ignore the mixins.