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

查看:31
本文介绍了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?

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

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天全站免登陆