翻译自定义错误消息



我有一个表单(使用simple_form),我想实现对翻译错误消息的支持。我所有的翻译出现在错误消息外,

我的客户模型是:

class Customer < ActiveRecord::Base
  attr_accessible :name, :phone, :email, :contact_method
  validates_presence_of :phone, :email, :contact_method, :message => I18n.t(:required)
end

我的fr.yml文件

fr:
  name: 'Nom'
  phone: 'Téléphone'
  email: 'Courriel'
  contact_method: 'Méthode de contact'
  required: 'Requis'

我的形式如下:

= simple_form_for @customer do |f|
  = f.input :name, label: t(:name)
  = f.input :phone, label: t(:phone)
  = f.input :email, label: t(:email)

我缺少什么?

首先,您应该使用带有validates_presence_ofSymbol。不要手动使用I18N翻译:

validates_presence_of :phone, :email, :contact_method, :message => :required

其次,将错误消息的转换添加到本地文件中:

activerecord:
  errors:
    models:
      customer:
        required: 'Requis'

最新更新