Active Model 错误
表示单个错误
方法
- A
- D
- F
- M
- N
- S
常量
CALLBACKS_OPTIONS | = | [:if, :unless, :on, :allow_nil, :allow_blank, :strict] |
MESSAGE_OPTIONS | = | [:message] |
属性
[R] | attribute | 错误所属的 |
[R] | base | 错误所属的对象 |
[R] | options | 调用 |
[R] | raw_type | 调用 |
[R] | type | 错误类型,除非指定,否则默认为 |
类公共方法
new(base, attribute, type = :invalid, **options) 链接
实例公共方法
details() 链接
返回错误详细信息。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }
别名: detail
full_message() 链接
返回完整的错误信息。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.full_message
# => "Name is too short (minimum is 5 characters)"
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 159 def full_message self.class.full_message(attribute, message, @base) end
match?(attribute, type = nil, **options) 链接
查看错误是否与提供的attribute
、type
和options
匹配。
未包含的参数不会被检查是否匹配。
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 166 def match?(attribute, type = nil, **options) if @attribute != attribute || (type && @type != type) return false end options.each do |key, value| if @options[key] != value return false end end true end
message() 链接
返回错误消息。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.message
# => "is too short (minimum is 5 characters)"
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 135 def message case raw_type when Symbol self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS)) else raw_type end end
strict_match?(attribute, type, **options) 链接
查看错误是否与提供的attribute
、type
和options
完全匹配。
所有参数都必须等于 Error 自身的属性,才被视为严格匹配。
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 184 def strict_match?(attribute, type, **options) return false unless match?(attribute, type) options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end
实例保护的方法
attributes_for_hash() 链接
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 204 def attributes_for_hash [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end