Performance: Why controllers handle requests
This affects how quickly the server processes incoming requests and prepares responses, impacting the time before the browser receives content.
class UsersController < ApplicationController def show @user = User.find(params[:id]) render json: @user end end
class UsersController < ApplicationController def show @user = User.all render json: @user end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetching all records in controller | N/A (server-side) | N/A | N/A | [X] Bad |
| Fetching only requested record | N/A (server-side) | N/A | N/A | [OK] Good |
show in a Rails controller?def action_name and end./welcome/greet?
class WelcomeController < ApplicationController
def greet
@message = "Hello, Rails!"
end
end
Assuming the view greet.html.erb contains:
<h1><%= @message %></h1>
greet sets @message to "Hello, Rails!" which is accessible in the view.<%= @message %> to insert the message text inside an <h1> element.class ProductsController < ApplicationController
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def create
product = Product.new(params[:product])
if product.save
redirect_to product_path(product)
else
render :new
end
end
endparams[:product] directly, which is unsafe and disallowed in modern Rails without strong parameters.product_params to permit only allowed attributes.new to display the form, and create to handle form submission and save data.Pass the test and this pattern moves unknown