我在rails4中为验证器定制消息时遇到了问题。我有设计注册系统的应用程序。我的模型中有一些验证器:
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当用户在姓名输入中留空时,会出现一条消息:
Name can't be blank当我向我的验证器添加消息选项时:
validates :name, length: {maximum: 45}, presence: {message: "Imię nie może być puste"}我有以下消息: Name Imięnie może byćpuste。我不想在我的消息中出现这个名字。该怎么做呢?
发布于 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"
https://stackoverflow.com/questions/19559027
复制相似问题