我正在使用 Ruby on Rails 3.0.9,我正在尝试验证嵌套模型。假设我对“主”模型运行验证并为嵌套模型生成一些错误,我得到以下信息:
@user.valid?
@user.errors.inspect
# => {:"account.firstname"=>["is too short", "can not be blank"], :"account.lastname"=>["is too short", "can not be blank"], :account=>["is invalid"]}
如何查看 RoR 框架创建了一个 errors
散列,其中包含以下键:account.firstname
、account.lastname
、帐户
。由于我想通过使用涉及 CSS 属性的 JavaScript(顺便说一句:我使用 jQuery)处理这些错误键值对来在前端内容上显示错误消息,我想“准备”该数据并将这些键更改为 account_firstname
, account_lastname
, account
(注意:我将 .
替换为 _
字符).
如何将键值从 account.firstname
更改为 account_firstname
?
而且,最重要的,我应该如何处理这种情况?我正在尝试做一种“好的”方式来处理嵌套模型错误吗?如果不是,那么常见\最佳方法是什么?
最佳答案
我做了一个快速关注,它显示了嵌套模型的完整错误消息:
https://gist.github.com/4710856
#1.9.3-p362 :008 > s.all_full_error_messages
# => ["Purchaser can't be blank", "Consumer email can't be blank", "Consumer email is invalid", "Consumer full name can't be blank"]
关于ruby-on-rails - 嵌套模型错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7050564/