2015-04-23 53 views
0

我有一個叫user.rb的模型。這種模式有一個自定義的電子郵件驗證:在rails上定製驗證消息

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, :lockable and :timeoutable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable #,:validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 

    validates_presence_of :email,:message=>"El campo email is requerido" 

end 

的問題是,瀏覽器顯示「電子郵件薩爾瓦多坎普電子郵件requerido」,而我想要的「厄爾尼諾坎普電子郵件是requerido」(不電子郵件之前)。

回答

0

對於比3.2.2更新的Rails,它使用國際化定義的errors.format來構建驗證完整消息。

我相信你不能只改變一個屬性的行爲,但如果想這樣做對整個應用程序,你可以按照我的回答對Customise ActiveModel full_messages

一種選擇是直接定義的消息記錄的base因此它可以被印刷,因爲它是:

validate do |user| 
    user.errors.add :base, 'El campo email is requerido' if user.email.blank? 
end 

此外,上拉請求是打開以提供屬性級別上的溶液https://github.com/rails/rails/pull/14260

0

可以使用I18n將電子郵件的翻譯更改爲「El campo email」

activerecord: 
    attributes: 
     user: 
     email: "El campo email"