我正在使用Ruby on Rails 3,我正在尝试按顺序to handle errors "a la Rails way"执行一个类帐户。
在我的模特中我有
class Users::Account
extend ActiveModel::Naming
extend ActiveModel::Translation
include ActiveModel::Validations
include ActiveModel::Conversion
def persisted?
false
end
attr_reader :errors
def initialize(attributes = {})
@errors = ActiveModel::Errors.new(self)
@firstname = attributes[:firstname]
@lastname = attributes[:lastname]
...
end
end
我想使用ActiveModel::Errors
---
errors:
base: Invalid account.
firstname: Too short.
这样我就可以在类中插入上面的错误哈希,就像这样
@account.errors # => Hash of errors
测试场景的调试是(总是)以下因为我不知道如何将错误附加到类中。
firstname: T
lastname: Test surname
errors: !omap []
我该怎么做?
答案 0 :(得分:-1)