instance method each

Ruby on Rails 3.2.22.5

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

each()

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.

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

p.errors.add(:name, "must be specified")
p.errors.each do |attribute, errors_array|
  # Will yield :name and "can't be blank"
  # then yield :name and "must be specified"
end
Source
# File activemodel/lib/active_model/errors.rb, line 151
    def each
      messages.each_key do |attribute|
        self[attribute].each { |error| yield attribute, error }
      end
    end

Defined in activemodel/lib/active_model/errors.rb line 151 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveModel::Errors

Type at least 2 characters to search.

↑↓ navigate · open · esc close