Rails' Four-Layer Contract: Why Every Feature Needs a Route, Policy, Controller, AND Model Method

Here's a scenario that should terrify you: a user clicks a button, sees no error, and walks away assuming their action succeeded. Meanwhile, on the server, something quietly did nothing — or worse,...

By · · 1 min read
Rails' Four-Layer Contract: Why Every Feature Needs a Route, Policy, Controller, AND Model Method

Source: DEV Community

Here's a scenario that should terrify you: a user clicks a button, sees no error, and walks away assuming their action succeeded. Meanwhile, on the server, something quietly did nothing — or worse, did the wrong thing. This isn't a hypothetical. It's what happens when you add a UI element to a Rails app without completing what I call the four-layer contract : Route → Policy → Controller → Model. Miss any single layer, and you get silent failures that won't show up in your logs or error trackers. Let's break this down layer by layer, look at exactly how each one fails, and build a checklist you can use every time you add a new action to your app. The Four Layers, Visualized Every user-initiated action in a Rails app travels through this chain: Browser Request │ ▼ ┌─────────────┐ │ Route │ Does this URL + verb map to an action? └──────┬──────┘ │ ▼ ┌─────────────┐ │ Policy │ Is this user allowed to do this? └──────┬──────┘ │ ▼ ┌─────────────┐ │ Controller │ Orchestrate: parse params, call