0

我对 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。我不想在我的消息中包含此名称字词。这个怎么做?

4

1 回答 1

0

在你的 config/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"

例如,不是显示“电子邮件不能为空白”,而是显示“不能为空白”。本质上,您将“名称:”替换为等于空字符串的别名。您可以将“姓名:”设置为姓名:“您的姓名”

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

它会显示“你的名字不能为空”

于 2017-01-13T01:40:13.107 回答