重新加载后如何保留验证错误消息?
我正在我的 foo 模型中运行多个验证。以下是我处理创建和更新操作的方式:
if @foo.update_attributes(params[:foo])
#party time
else
@foo.reload #here is the issue
render :action => "new" #or edit
end
我必须重新加载属性,因为我使用默认表单值来运行不相关的 javascript。问题是,通过重新加载属性,我显然丢失了@foo.errors。有没有办法可以将它们传递给新的重新加载的变量?我更喜欢使用 JavaScript 来防止错误的表单提交。谢谢!
I am running several validations in my foo model. Here is how I am handling the create and update actions:
if @foo.update_attributes(params[:foo])
#party time
else
@foo.reload #here is the issue
render :action => "new" #or edit
end
I have to reload the attributes because I am using the default form values to run unrelated javascript. The problem is that by reloading the attributes, I apparently lose @foo.errors. Is there a way that I can pass them to the new reloaded variable? I would prefer this over using javascript to prevent the bad form submission. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
reload方法显然不适合这个。你可以:
1. 定义恢复值的方法
2.保存错误然后重新分配
3. 将属性从新加载的实例合并到@foo。
所有这些解决方案都非常hackish,不推荐。如果我是你,我会重新思考事情应该如何运作。
The reload method is obviously not for that. You can:
1. define your method for reverting the values
2. save the errors and then re-assign
3. merge attributes to the @foo from freshly loaded instance.
All of these solutions are very hackish and are not recommended. If I were you, I'd rethink the way things should work.