Rails 覆盖验证消息 [英] Rails override validation message

查看:62
本文介绍了Rails 覆盖验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在验证消息中看到无效值.

I want to see not valid value in validation message.

validates_uniqueness_of :event, :scope => :user_id

结果:标题已被占用"我想:事件#{event} 已经被#{user} 占用"

Result: "Title has already has been taken" I want: "Event #{event} already has been taken by #{user}"

我正在尝试这样做,面包不起作用:

i'm trying do this way, bun not working:

validates_uniqueness_of :event, :scope => :user_id, :message=>"#{self.event} already has been taken by #{self.user}"

推荐答案

来自 ActiveRecord 源代码注释:

From the ActiveRecord source code comment:

值 :model, :attribute 和:value 始终可用插值 :count 是适用时可用.可以使用用于复数.

The values :model, :attribute and :value are always available for interpolation The value :count is available when applicable. Can be used for pluralization.

所以你可以简单地写下你的消息

So you can simply write your message as

validates_uniqueness_of :event, :scope => :user_id, 
                        :message=>"{{value}} is already taken"

这篇关于Rails 覆盖验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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