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:

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:

person = Person.new
person.validate!            # => ["cannot be nil"]
person.errors.full_messages # => ["name cannot be nil"]
# etc..
[]

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

2015-06-20 00:00:00
full_message

full_message(attribute, message) Instance Public methods Returns a full message

2015-06-20 00:00:00
full_messages_for

full_messages_for(attribute) Instance Public methods Returns all the full error

2015-06-20 00:00:00
new

new(base) Class Public methods Pass in the instance of the object that is using

2015-06-20 00:00:00
to_xml

to_xml(options={}) Instance Public methods Returns an xml formatted representation

2015-06-20 00:00:00
count

count() Instance Public methods Returns the number of error messages.

2015-06-20 00:00:00
to_a

to_a() Instance Public methods Returns an array of error messages, with the

2015-06-20 00:00:00
each

each() Instance Public methods Iterates through each error key, value pair in

2015-06-20 00:00:00
empty?

empty?() Instance Public methods Returns true if no errors are

2015-06-20 00:00:00
[]=

[]=(attribute, error) Instance Public methods Adds to the supplied attribute

2015-06-20 00:00:00