跳至内容 跳至搜索

Active Model 验证错误

当模型无效时,由 validate! 抛出。使用 model 方法检索未通过验证的记录。

begin
  complex_operation_that_internally_calls_validate!
rescue ActiveModel::ValidationError => invalid
  puts invalid.model.errors
end
方法
N

属性

[R] model

类公共方法

new(model)

# File activemodel/lib/active_model/validations.rb, line 496
def initialize(model)
  @model = model
  errors = @model.errors.full_messages.join(", ")
  super(I18n.t(:"#{@model.class.i18n_scope}.errors.messages.model_invalid", errors: errors, default: :"errors.messages.model_invalid"))
end