Error messages help users know what went wrong when they fill out forms or use your app. Showing these messages clearly makes your app easier and friendlier to use.
Error messages and display in Ruby on Rails
if @model.errors.any? @model.errors.full_messages.each do |message| # display message end end
@model.errors.any? checks if there are any errors on the model.
@model.errors.full_messages gives a list of readable error messages.
<% if @user.errors.any? %> <div class="error-messages"> <h2><%= pluralize(@user.errors.count, "error") %> prevented this user from saving:</h2> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
<%= form_with model: @post do |form| %> <%= form.label :title %> <%= form.text_field :title %> <%= form.error_message_on :title %> <%= form.submit %> <% end %>
This example shows a simple Rails form for creating a user. If the user submits invalid data, error messages appear above the form. The form fields have ARIA attributes for accessibility.
# app/controllers/users_controller.rb class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(user_params) if @user.save redirect_to root_path, notice: "User created successfully" else render :new end end private def user_params params.require(:user).permit(:name, :email) end end # app/views/users/new.html.erb <h1>New User</h1> <% if @user.errors.any? %> <div aria-live="polite" role="alert" class="error-messages"> <h2><%= pluralize(@user.errors.count, "error") %> prevented this user from saving:</h2> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= form_with model: @user do |form| %> <div> <%= form.label :name %><br> <%= form.text_field :name, aria: { describedby: 'name_error' } %> </div> <div> <%= form.label :email %><br> <%= form.email_field :email, aria: { describedby: 'email_error' } %> </div> <div> <%= form.submit "Create User" %> </div> <% end %>
Always use aria-live="polite" and role="alert" on error message containers for screen readers.
Use pluralize helper to show correct singular/plural wording.
Place error messages near the related form fields for better user guidance.
Error messages tell users what went wrong and how to fix it.
Use @model.errors.full_messages to get readable messages.
Show errors clearly and accessibly in your views.
Practice
Solution
Step 1: Understand error message methods
@model.errors.full_messagesreturns an array of readable error strings, while others return metadata or counts.Step 2: Identify user-friendly output
Onlyfull_messagesgives clear, complete error messages suitable for display.Final Answer:
@model.errors.full_messages -> Option DQuick Check:
Error messages = full_messages [OK]
- Using errors.details which gives error codes, not messages
- Using errors.keys which lists fields, not messages
- Using errors.count which returns number, not messages
@user model in a Rails view?Solution
Step 1: Understand ERB tags for iteration
<% %>runs Ruby code without output;<%= %>outputs the result. For loops, use<% %>.Step 2: Identify correct syntax for displaying messages
<% @user.errors.full_messages.each do |msg| %>correctly uses<%= msg %>
<% end %><% %>to loop and<%= %>inside to output each message.Final Answer:
Option B -> Option BQuick Check:
Loop with <% %>, output with <%= %> [OK]
- Using <%= %> for loops causing unwanted output
- Using map without outputting results
- Mixing output and code tags incorrectly
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>What will be the output if
@post.errors.full_messages contains ["Title can't be blank", "Content is too short"]?Solution
Step 1: Understand the loop output
The code loops over each error message and wraps it in- tags inside a <ul>.
Step 2: Match output to HTML structure
The output will be a <ul> list with two- items showing each error message.
Final Answer:
<ul><li>Title can't be blank</li><li>Content is too short</li></ul> -> Option AQuick Check:
Loop creates list items inside ul [OK]
- Forgetting <ul> tags causing invalid HTML
- Not using <li> tags for each message
- Confusing output with plain text
<%= @article.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>But no errors show up in the browser. What is the problem?
Solution
Step 1: Check ERB tag usage for loops
Using<%= %>each, which is the original array, not the loop content.Step 2: Correct loop tag usage
Use<% %>for loops to run code without output, then use<%= %>inside for each message.Final Answer:
Using <%= %> for the loop causes output of the array, not the messages -> Option CQuick Check:
Use <% %> for loops, not <%= %> [OK]
- Using <%= %> for loops causing wrong output
- Assuming errors are empty without checking
- Misunderstanding HTML tag usage inside loops
@comment model. Which is the best way to do this in a Rails view?Solution
Step 1: Check for errors presence
Useerrors.any?to check if there are any errors to display.Step 2: Display errors accessibly
Wrap errors in adivwithrole='alert'andaria-live='assertive'for screen readers to announce changes.Step 3: Loop and show messages
Usefull_messagesinside aulwithlifor each error message.Final Answer:
Option A -> Option AQuick Check:
Check errors.any? and use accessible alert div [OK]
- Not checking if errors exist before displaying
- Missing accessibility roles for error containers
- Displaying generic messages without details
Ready to prove it?
Pass the test and this pattern moves unknown
