each(&block) public

Iterates through each error object.

person.errors.add(:name, :too_short, count: 2)
person.errors.each do |error|
  # Will yield <#ActiveModel::Error attribute=name, type=too_short,
                                    options={:count=>3}>
end

To be backward compatible with past deprecated hash-like behavior, when block accepts two parameters instead of one, it iterates through each error key, value pair in the error messages hash. Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.

person.errors.add(:name, :blank, message: "can't be blank")
person.errors.each do |attribute, message|
  # Will yield :name and "can't be blank"
end

person.errors.add(:name, :not_specified, message: "must be specified")
person.errors.each do |attribute, message|
  # Will yield :name and "can't be blank"
  # then yield :name and "must be specified"
end
Show source
Register or log in to add new notes.