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.
0
0
Error messages and display in Ruby on Rails
Introduction
When a user submits a form with missing or wrong information
When saving data fails because of validation rules
When you want to guide users to fix mistakes in their input
When you want to show success or failure feedback after an action
When debugging or logging errors during development
Syntax
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.
Examples
This example shows how to display all error messages for a user in a list inside a view.
Ruby on Rails
<% 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 %>
This example shows how to display an error message next to a specific form field (using a helper method).
Ruby on Rails
<%= form_with model: @post do |form| %> <%= form.label :title %> <%= form.text_field :title %> <%= form.error_message_on :title %> <%= form.submit %> <% end %>
Sample Program
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.
Ruby on Rails
# 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 %>
OutputSuccess
Important Notes
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.
Summary
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.