Performance: MVC architecture in Rails
MVC architecture in Rails affects how quickly the server can respond and how efficiently the browser renders the page by organizing code and data flow.
class UsersController < ApplicationController def index @users = User.select(:id, :name).limit(50) render :index end end
class UsersController < ApplicationController def index @users = User.all @posts = Post.all render :index end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Loading all data in controller | N/A (server-side) | N/A | N/A | [X] Bad |
| Selective data loading with limits | N/A (server-side) | N/A | N/A | [OK] Good |
class ClassName < ParentClass and closed with end.ApplicationController using < syntax.< [OK]/welcome/index?
class WelcomeController < ApplicationController
def index
@message = "Hello, Rails!"
end
end
And the view file app/views/welcome/index.html.erb contains:
<h1><%= @message %></h1>
@message to "Hello, Rails!" which is accessible in the view.<%= @message %> to insert the string value inside the <h1> tag.<%= %> [OK]= in ERB tagclass ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
end
def index
@products = Product.all
end
end
What is the likely cause of a runtime error when visiting /products/show without an id parameter?show method usageshow method calls Product.find(params[:id]), which requires a valid id parameter.params[:id] is missing, find raises an ActiveRecord::RecordNotFound error causing a runtime failure.id in params causes find to raise an exception -> Option Bid in params causes find to raise an exception [OK]find [OK]Pass the test and this pattern moves unknown