我在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"}
我有以下消息:姓名Imięniemożebyćpuste。 我不希望在我的消息中有这个名字。怎么做?
答案 0 :(得分: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"
例如,不显示“电子邮件不能为空”,而是显示 “不能空白”。实际上,您要使用等于空字符串的别名替换“name:”。您可以将“name:”设置为名称:“您的名字”
en:
activerecord:
attributes:
user:
name: "Your name"
它会显示“你的名字不能为空”