首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义rails错误消息

自定义rails错误消息
EN

Stack Overflow用户
提问于 2013-10-24 14:56:59
回答 1查看 75关注 0票数 0

我在rails4中为验证器定制消息时遇到了问题。我有设计注册系统的应用程序。我的模型中有一些验证器:

代码语言:javascript
复制
 validates :name, length: {maximum: 45}, presence: true
 validates :surname, length: {maximum: 45}, presence: true
 validates :phone, :phony_plausible => true, presence: true
 validates :company_name, length: {maximum: 100}, presence: true
 validates :address, length: {maximum: 50}, presence: true
 validates :city, length: {maximum: 70}, presence: true
 validates :zip_code, presence: true, length: {is: 6}
 validates :nip, nip: true

当用户在姓名输入中留空时,会出现一条消息:

代码语言:javascript
复制
Name can't be blank

当我向我的验证器添加消息选项时:

代码语言:javascript
复制
  validates :name, length: {maximum: 45}, presence: {message: "Imię nie może być puste"}

我有以下消息: Name Imięnie może byćpuste。我不想在我的消息中出现这个名字。该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2017-01-13 09:40:13

在您的配置文件/locales/en.yml中

en: activerecord: attributes: [model_name_goes_here]: [attribute_name_goes here]: "" errors: models: [model_name_goes_here]: attributes: [attribute_name_goes_here]: blank: "Email can't be blank"

示例:

en: activerecord: attributes: user: email: "" errors: models: veteran: attributes: email: blank: "Email can't be blank"

例如,它不会显示“电子邮件不能为空”,而会显示“不能为空”。实际上,您是在用一个等同于空字符串的别名替换"name:“。您可以将" name:“设置为name:"Your name”

en: activerecord: attributes: user: name: "Your name"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19559027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档