【发布时间】:2016-03-24 15:52:41
【问题描述】:
在我的 Rails 4 应用程序中,我在 Post 模型上实现了一个名为 LinkValidator 的自定义验证器:
class LinkValidator < ActiveModel::Validator
def validate(record)
if record.format == "Link"
if extract_link(record.copy).blank?
record.errors[:copy] << 'Please make sure the copy of this post includes a link.'
end
end
end
end
一切正常,除了目前显示的消息是:
1 error prohibited this post from being saved:
Copy Please make sure the copy of this post includes a link.
如何去掉上面留言中的“复制”二字?
我在验证器中尝试了record.errors << '...' 而不是record.errors[:copy] << '...',但验证不再有效。
有什么想法吗?
【问题讨论】:
-
将错误添加到
base而不是copy -
非常感谢您的评论。不过,我不确定我是否跟随。你的意思是我应该写
record.errors[:base]而不是record.errors[:copy]? -
是的,没错。看guides.rubyonrails.org/…
-
完美,这确实解决了问题。你想建议这个作为答案吗?我很乐意接受。
-
没关系。这很可能是重复的,但我似乎找不到重复的问题。
标签: ruby-on-rails validation ruby-on-rails-4 activemodel