Back to articles
The "Middle" of Nowhere: Demystifying Middleware in Rails

The "Middle" of Nowhere: Demystifying Middleware in Rails

via Dev.to WebdevZil Norvilis

The "Black Box" Between the Browser and Your Code When you are learning Rails, you usually think the flow works like this: User clicks a link. Magic happens. Your PostsController runs. Today, we are going to look at the Magic . That magic is called Middleware . Understanding Middleware is the difference between a Junior dev who thinks "Rails just does that" and a Senior dev who knows exactly how Rails does that. The Mental Model: The Onion The best way to visualize Middleware is the Onion Architecture . Imagine your application logic (your Controller) is the core of the onion. The user's request is a needle trying to reach the center. The request pierces the outer skin (Middleware 1). It goes through the next layer (Middleware 2). It reaches the center (Your Controller). Crucially: The response then has to travel back out through Middleware 2, then Middleware 1, before leaving the server. This means Middleware can modify the Request coming in, AND the Response going out. What actually

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
2 views

Related Articles