Performance: Error messages and display
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by how error messages are rendered and updated in the UI.
Render error messages inline next to each input field using small containers that update only changed parts. <%= form_with model: @user do |f| %> <div> <%= f.label :email %> <%= f.text_field :email %> <% if @user.errors[:email].any? %> <span class="error-message" role="alert"><%= @user.errors[:email].first %></span> <% end %> </div> <% end %>
In the view, render all error messages inside a large container that is hidden by default and shown by toggling CSS display, causing full reflow. <% if @user.errors.any? %> <div id="error_explanation" style="display:block;"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Toggling large error container visibility | Modifies one large container node | 1 full reflow triggered | High paint cost due to large area | [X] Bad |
| Inline error message updates next to inputs | Modifies small inline nodes | Localized reflow only | Low paint cost | [OK] Good |