How can I add errors to the User
model manually?
如何手动向用户模型添加错误?
Is it just @user.errors << "some text goes here"
?
只是@user。<< "有文字到这里来" ?
4 个解决方案
#1
85
@user.errors.add(:email, "Not valid")
If you don't want to use any attributes, then in your model
如果您不想使用任何属性,那么在您的模型中
@user.errors[:base] << "This person is invalid because ..."
For details: link
详情:链接
#2
9
WARNING
警告
If you just add errors in a separate method (not in a validation), then by default when you call .valid?
or .save
those errors will be automatically cleared. So you may want to use validation contexts instead.
如果您只是在单独的方法中添加错误(而不是在验证中),那么在默认情况下,当您调用.valid时?或者。save这些错误将被自动清除。因此,您可能需要使用验证上下文。
#4
2
try this:
试试这个:
errors.add(:base, "#{user.full_name} has errors here!")
#1
85
@user.errors.add(:email, "Not valid")
If you don't want to use any attributes, then in your model
如果您不想使用任何属性,那么在您的模型中
@user.errors[:base] << "This person is invalid because ..."
For details: link
详情:链接
#2
9
WARNING
警告
If you just add errors in a separate method (not in a validation), then by default when you call .valid?
or .save
those errors will be automatically cleared. So you may want to use validation contexts instead.
如果您只是在单独的方法中添加错误(而不是在验证中),那么在默认情况下,当您调用.valid时?或者。save这些错误将被自动清除。因此,您可能需要使用验证上下文。
#3
#4
2
try this:
试试这个:
errors.add(:base, "#{user.full_name} has errors here!")