1

我有一个窗体(使用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) 

有我丢失的东西?

回答

3

首先,你应该使用Symbolvalidates_presence_of。不要将其与手动的I18n翻译:

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

其次,对于您的错误信息添加翻译的语言环境的文件是这样的:

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

优秀。谢谢! – dspencer

+0

很高兴帮助:) –