Ruby on Rails:errors.add_to_base 与 errors.add [英] Ruby on Rails: errors.add_to_base vs. errors.add

查看:17
本文介绍了Ruby on Rails:errors.add_to_base 与 errors.add的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过,errors.add_to_base 应该用于与对象相关的错误,而不是特定的属性.我在概念化这意味着什么时遇到了麻烦.有人可以举个例子说明我什么时候想使用它们吗?

I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an example of when I would want to use each?

例如,我有一个乐队模型,每个乐队都有一个流派.当我验证某个类型是否存在时,如果该类型丢失,是否应该将错误添加到基础中?

For example, I have a Band model and each Band has a Genre. When I validate the presence of a genre, if the genre is missing should the error be added to the base?

例子越多越好

谢谢!

推荐答案

缺少类型将是字段错误.基本错误类似于现有记录的完全重复,其中问题与任何特定字段无关,而是与整个记录有关(或至少与某些字段组合有关).

A missing genre would be a field error. A base error would be something like an exact duplicate of an existing record, where the problem wasn't tied to any specific field but rather to the record as a whole (or at lest to some combination of fields).

这篇关于Ruby on Rails:errors.add_to_base 与 errors.add的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆