2016-08-08 126 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