如何在 Rails 3 中存储带有验证错误消息的额外数据?
我试图在 Rails 3 的自定义验证器中存储一些附加数据和标准错误消息。
例如,(忽略内置验证器)假设我想在保存之前检查帖子是否重复。我可能会像这样编写自定义验证方法:
class Post < ActiveRecord::Base
# prevent duplicate posts
validate do |post|
duplicates = Post.find_all_by_body(body)
errors.add_to_base("Post is a duplicate!") if duplicates.length
# something like this is desired:
# errors.add_to_base("Post is a duplicate",
# :extra => { :duplicates => duplicates })
end
end
这会让用户知道存在重复项,但除了添加错误消息之外,我还想存储重复项,以便将它们显示给用户。如何存储在验证期间检索到的重复帖子列表,使其与 body
字段的记录错误相关联,并可供我查看?
一个更简单的例子可能是长度验证:如果一个字段超过了它的最大长度,我如何将最大长度与错误消息一起存储,而不像 Rails 目前那样简单地将其插入到消息中?
I'm trying to store some additional data along with the standard error message in a custom validator in Rails 3.
For example, (ignoring built-in validators) suppose I wanted to check to see if a post is a duplicate before it's saved. I might write my custom validation method like this:
class Post < ActiveRecord::Base
# prevent duplicate posts
validate do |post|
duplicates = Post.find_all_by_body(body)
errors.add_to_base("Post is a duplicate!") if duplicates.length
# something like this is desired:
# errors.add_to_base("Post is a duplicate",
# :extra => { :duplicates => duplicates })
end
end
This will let the user know there are duplicates, but along with adding the error message I would also like to store the duplicates so they can be displayed to the user. How would I store the list of duplicate posts retrieved during validation such that it is associated with the record's errors for the body
field, and available to my view?
A simpler example might be length validation: If a field exceeds its maximum length, how can I store the maximum length along with an error message without simply interpolating it into the message as Rails currently does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前不必这样做,但我的第一个想法是在对象上创建一个名为重复的新方法。
然后,在自定义验证方法中,您可以在对象上设置重复项,以便在渲染错误时使它们可用于视图。请注意,您当前的代码没有太大变化:
然后您必须手动拦截视图中的错误,以便在“帖子是重复的!”时可以打印出所有重复项。遇到错误。
I have not had to do this before, but my first thought is to create a new method on the object called duplicates.
Then in your custom validate method, you can set the duplicates on the object making them available to the view when you render the errors. Notice your current code doesn't change much:
You would then have to intercept that error in the view manually so that you can print out all the duplicates if the "Post is a duplicate!" error is encountered.
您可以将选项传递给错误,但它们仅用作 i18n 模板中的替换。长话短说,不,您不能将有关错误的元数据存储在错误哈希中。如果您需要这样的功能,您需要查看 Rails 核心中的 ActiveModel::Errors 模块。
更新:
另一种解决方案可能是,不是将字符串推入错误哈希,而是填充自己的类的实例,该类像字符串一样嘎嘎作响,但会用额外的方法和状态等进行装饰。
You can pass options to the error but they are only used as substitution in i18n templates. To make a long story short, no you can't store meta-data about your error in the errors hash. If you need such a functionality you'll need to look into the ActiveModel::Errors module in Rails core.
Update:
Another solution could be that instead of pushing a string into error hash, you stuff an instance of your own class, a class which quacks like a string but would be decorated with extra methods and state and such like.