Type:
Class
Constants:
CALLBACKS_OPTIONS : [:if, :unless, :on, :allow_nil, :allow_blank, :strict]

Active Model Errors

Provides a modified Hash that you can include in your object for handling error messages and interacting with Action View helpers.

A minimal implementation could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Person
  # Required dependency for ActiveModel::Errors
  extend ActiveModel::Naming
 
  def initialize
    @errors = ActiveModel::Errors.new(self)
  end
 
  attr_accessor :name
  attr_reader   :errors
 
  def validate!
    errors.add(:name, "cannot be nil") if name == nil
  end
 
  # The following methods are needed to be minimally implemented
 
  def read_attribute_for_validation(attr)
    send(attr)
  end
 
  def Person.human_attribute_name(attr, options = {})
    attr
  end
 
  def Person.lookup_ancestors
    [self]
  end
end

The last three methods are required in your object for Errors to be able to generate error messages correctly and also handle multiple languages. Of course, if you extend your object with ActiveModel::Translation you will not need to implement the last two. Likewise, using ActiveModel::Validations will handle the validation related methods for you.

The above allows you to do:

1
2
3
4
person = Person.new
person.validate!            # => ["cannot be nil"]
person.errors.full_messages # => ["name cannot be nil"]
# etc..
clear
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

clear() Instance Public methods Clear the error messages.

2025-01-10 15:47:30
to_hash
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

to_hash(full_messages = false) Instance Public methods Returns a

2025-01-10 15:47:30
add_on_empty
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

add_on_empty(attributes, options = {}) Instance Public methods Will add an error

2025-01-10 15:47:30
as_json
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

as_json(options=nil) Instance Public methods Returns a

2025-01-10 15:47:30
set
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

set(key, value) Instance Public methods Set messages for key to

2025-01-10 15:47:30
full_messages
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

full_messages() Instance Public methods Returns all the full error messages

2025-01-10 15:47:30
keys
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

keys() Instance Public methods Returns all message keys.

2025-01-10 15:47:30
generate_message
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

generate_message(attribute, type = :invalid, options = {}) Instance Public methods Translates

2025-01-10 15:47:30
has_key?
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

has_key?(attribute) Instance Public methods aliases include?

2025-01-10 15:47:30
[]
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Errors

[](attribute) Instance Public methods When passed a symbol or a name of a method

2025-01-10 15:47:30