如果不保存记录,如何在表单上显示用户电子邮件的错误消息
有没有一种方法可以将错误消息添加到属性并在表单上显示它们,而无需尝试保存对象。
经理可能希望更改分配给项目的用户的电子邮件。
如果他们将电子邮件更改为已分配给项目的其他用户的电子邮件,我想向用户电子邮件属性添加错误并显示它。
在这种情况下,我不会保存记录或更新它,我只会返回表单并显示错误。
我是否只是
@user.add(:email, "This user is already assigned to this project")
不运行@user.save或@user.update_attribute,在这种情况下,该用户没有被更新,而是被分配给项目。
我不只是想显示一条即时消息,因为我知道我可以做到这一点。我希望该字段显示错误。
这可能吗?
Is there a way to add error messages to an attribute and show them on the form without trying to save the object.
A manager may wish to change the email of a user assigned to a project.
If they change the email to that of another user who is already assigned to a project I want to add an error to the user email attribute and display it.
In this case I would not save the record or update it I would just go back to the form and show the error.
Do I just do
@user.add(:email, "This user is already assigned to this project")
I don't run @user.save or @user.update_attribute, this user is not being updated but being assigned to the project in this case.
I don't just want to show a flash message as I know I could do this. I want the field to show the error.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,请在模型上调用
valid?
。Sure, call
valid?
on the model.