显示用户友好的Devise嵌套验证错误

时间:2015-01-20 07:43:11

标签: ruby-on-rails devise rails-activerecord nested-forms nested-attributes

我的Devise注册页面上有一个嵌套表单。我有一个通过角色模型链接到用户的公司模型。

请点击此处查看相关帖子:How do I update a nested resource without passing the data via a form

我可以设置我的验证等,但它们工作得很好,但是当嵌套资源显示错误消息时,它变得很丑陋 - 字面意思:

例如,如果我提交一份空白表格,我会:

>     Email can't be blank
>     Password can't be blank
>     Roles company name can't be blank
>     Roles company address1 can't be blank

前两个是非嵌套的表单元素,后两个是描述性的但很难用户友好。他们应该是公司名称'和'公司地址'可以;是空白等。

如果没有一些庞大的复杂代码,我可以改变它吗?

更新

我可能必须手动覆盖Devise错误消息帮助程序:

https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb

我想要清理格式化。

更新2

我做了一些挖掘,我发现了这个:

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

从我收集到的内容中,我无法在我的en.yml中找到它:

en:
    activerecord:
        attributes:
            user/role/company:
                address1: "First Name"
                name: "Company Name"

我试过了,但到目前为止没有运气。

更新3

我挖的越多,我发现的越多。这可能是最好的解决方案:

https://github.com/jeremydurham/custom-err-msg

这个gem(尽管多年没有维护)允许你完全删除模型属性。我可以走这条路线,无论如何只是定义自定义错误信息。

1 个答案:

答案 0 :(得分:1)

" custom-err-msg" Custom error Gem并不真正依赖于rails版本。因此,最好使用或以其他方式自己覆盖full_messages方法,或者在父模型本身中添加嵌套模型验证。