๐ Ruby on Rails Action Controller — The Complete Deep Dive Guide (From Request to Response!)
๐ Ruby on Rails Action Controller — The Complete Deep Dive Guide (From Request to Response!)
When a user clicks a button on your Rails app…
๐ก What exactly happens behind the scenes?
How does the request travel from the browser → server → router → controller → model → view → and back?
Today, we’ll break down Ruby on Rails Action Controller in full depth — step by step, class by class, parameter by parameter.

๐ 1️⃣ The Journey Begins: HTTP Request
When a user hits:
https://yourapp.com/posts/1The browser sends an HTTP request like:
GET /posts/1 HTTP/1.1This request contains:
- ๐ฃ Path:
/posts/1 - ๐ HTTP Verb:
GET - ๐ฆ Headers
- ๐งพ Cookies
- ๐งฎ Query Parameters
- ๐ Session info
Now Rails takes over.
๐ฅ 2️⃣ Rack — The Entry Point
Rails sits on top of Rack (Ruby Webserver Interface).
Flow:
Web Server (Puma)
↓
Rack Middleware Stack
↓
Rails ApplicationRails app responds to:
call(env)Where:
env= Huge Hash containing request details.
Example:
env["REQUEST_METHOD"] # => "GET"
env["PATH_INFO"] # => "/posts/1"๐ฃ 3️⃣ Routing Layer — config/routes.rb
The request now reaches:
# config/routes.rb
resources :postsThis automatically generates:

So:
GET /posts/1Matches:
PostsController#showRouting internally creates:
{
controller: "posts",
action: "show",
id: "1"
}This hash becomes params later.
๐ฏ 4️⃣ Controller Instantiation
Now Rails loads:
class PostsController < ApplicationController
endImportant hierarchy:
ActionController::Base
↑
ApplicationController
↑
PostsControllerWhat ActionController::Base Provides:
- Params handling
- Sessions
- Cookies
- Rendering
- Redirecting
- Filters (before_action)
- Strong Parameters
- CSRF Protection
⚙️ 5️⃣ Dispatch Process — Action Execution
Internally Rails runs something like:
PostsController.action(:show).call(env)Which:
- Creates controller instance
- Sets request & response objects
- Runs callbacks
- Executes action method
๐ง 6️⃣ The Action Method
Example:
def show
@post = Post.find(params[:id])
end๐น Where does params come from?
Rails builds params from:
- Route parameters (
:id) - Query parameters (
?page=2) - POST body parameters
Example:
/posts/1?page=2params = {
"id" => "1",
"page" => "2",
"controller" => "posts",
"action" => "show"
}params is an instance of:
ActionController::Parameters๐ 7️⃣ Strong Parameters (Security Layer)
Rails protects mass assignment.
Example:
def post_params
params.require(:post).permit(:title, :content)
endWhy?
Because without this:
Post.create(params[:post])Could allow malicious fields like:
admin: trueStrong parameters prevent that ๐ซ
๐ 8️⃣ Before, After & Around Actions
before_action :authenticate_user!
after_action :log_request
around_action :measure_timeExecution Flow:
before_action
↓
action method
↓
after_actionAround action wraps:
def measure_time
start = Time.now
yield
puts Time.now - start
end๐ช 9️⃣ Sessions & Cookies
Cookies:
cookies[:user_id] = 10Sessions:
session[:user_id] = 10Sessions are stored:
- Cookie Store (default)
- Redis
- Database
- Cache Store
๐จ ๐ Rendering Response
After action completes, Rails decides:
Case 1️⃣: Implicit Render
def show
@post = Post.find(params[:id])
endRails automatically renders:
app/views/posts/show.html.erbCase 2️⃣: Explicit Render
render :edit
render json: @post
render plain: "Hello"
render status: 404Case 3️⃣: Redirect
redirect_to posts_pathSends:
HTTP 302 Redirect๐งฉ 1️⃣1️⃣ The Full Internal Processing Flow
Here’s the complete lifecycle:
1. Browser sends HTTP request
2. Web Server (Puma) receives
3. Rack middleware stack processes
4. Rails Router matches route
5. Controller class loaded
6. Controller instance created
7. Params constructed
8. before_actions run
9. Action method executes
10. Model interaction happens
11. View rendered
12. Response returned to browser๐ 1️⃣2️⃣ Important Classes in Action Controller
๐ฆ Core Classes

๐งช 1️⃣3️⃣ Example Full Flow (POST Request)
POST /postsBody:
{
"post": {
"title": "Rails",
"content": "Powerful framework"
}
}Controller:
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render :new
end
endProcessing Steps:
- Route matches
create - Params extracted
- Strong params applied
- Model validation runs
- DB insert happens
- Redirect or render
๐ฅ 1️⃣4️⃣ API Mode Controllers
If using:
class PostsController < ActionController::APIRails:
- Skips view rendering
- Skips cookies by default
- Optimized for JSON APIs
Used in microservices & SPA backends.
๐ก 1️⃣5️⃣ CSRF Protection
Rails automatically adds:
protect_from_forgery with: :exceptionPrevents:
- Cross-Site Request Forgery attacks
Works using:
- Authenticity token
- Session verification
๐ 1️⃣6️⃣ Performance Tips
✅ Use before_action wisely
✅ Avoid heavy logic in controllers
✅ Use service objects
✅ Use eager loading
✅ Prefer head :ok when no body needed
๐ฏ 1️⃣7️⃣ Best Practices
✔ Keep controllers thin
✔ Move business logic to models/services
✔ Use strong params always
✔ Use RESTful routes
✔ Avoid N+1 queries
๐ก Final Mental Model
Think of Action Controller as:
๐ The Traffic Police of Your Rails App
It:
- Receives request ๐ฆ
- Verifies identity ๐
- Extracts data ๐ฆ
- Calls business logic ๐ง
- Sends response ๐ค
Without it, Rails app has no direction.
๐ Final Words
If you master Action Controller, you understand:
- How requests flow
- How security works
- How rendering works
- How Rails truly processes web requests
And that’s where you become not just a Rails developer…
But a Rails Architect ๐๐ฅ
Comments
Post a Comment