2013-07-10 31 views
6

我在我的紅寶石軌道應用程序中使用設計寶石。在用戶註冊時,如果電子郵件已存在,則會有一個默認消息「電子郵件已被使用」。更改設計消息「電子郵件已被採納」

我在en.yml改變了這一消息

activerecord: 
    errors: 
     messages: 
     taken: "User with same email already exists. Please try with another email address." 

鑑於我已經使用:

<div class="notice"><%= devise_error_messages! %></div> 

現在我得到的消息是

"Email User with same email already exists. Please try with another email address." 

的問題是在開始處附加「電子郵件」。

是否有任何其他方式來更改此默認消息?該消息的到

en: 
    errors: 
    format: "%{message}" 

默認

+0

另一種選擇:https://stackoverflow.com/questions/9006270/where-can-i-change-the-email已經被採取的錯誤消息 – user1515295

回答

7

更改格式是"%{attribute} %{message}"

UPDATE

還有另一種解決方案。我知道這是一個解決方法,但這裏.. 刪除現有的驗證,並添加一個自定義的。

validate :email_uniqueness 

def email_uniqueness 
    self.errors.add(:base, 'User with same email already exists. Please try with another email address.') if User.where(:email => self.email).exists? 
end 

注: 您應該考慮現有的用戶,同時做一個更新

+0

感謝您的回覆。它的工作。我可以只更改此消息的格式嗎? – Arif

+0

AFIK,你不能。您將不得不插入所有其他消息以包含屬性名稱。或者,而不是更改默認格式,您可以在錯誤消息中執行一些正則表達式替換。 – Santhosh

+0

請檢查我的編輯 – Santhosh

相關問題