2016-08-08 125 views
1

我有三種型號AccountUserAccountPermission嵌套屬性中的錯誤消息

我在創建用戶時通過AccountPermission創建Account

但是,如果發生與Account:name相關的問題,系統會拋出類似於下面的內容。

Account permissions account name has already been taken

所以,我只需要修正此錯誤消息。

我試着給我的驗證添加一個消息屬性。它只是附加到實際的消息。

我也試過locale的東西。還只是追加

en: 
    activerecord: 
    errors: 
     models: 
     account: 
      attributes: 
      name: 
       taken: 'bla bla' 

據我所知,在ActiveModel。此消息的結構從下面

區域/ en.yml加載ActiveModel中

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

那麼,有什麼辦法編輯此消息無痛人流?如果我甚至刪除了模型名稱,這就夠了。

+0

你要問你可以拋出一個自定義錯誤消息時驗證失敗? –

回答

1

錯誤消息從錯誤本身和屬性名稱(在提供的錯誤中爲account_permissions/account.name)連接在一起。

您可以爲您的屬性名稱添加區域,像這樣:

en: 
    activerecord: 
    attributes: 
     account: # this is model name 
     name: "Name" 

en: 
    activerecord: 
    attributes: 
     account_permissions/account: 
     name: "Account name" 
+0

終於!非常感謝。 'account_permissions/account'工作:) – utkuDAT