如何从 rails 控制台获取详细的错误消息? [英] How to get detailed error messages from rails console?

查看:50
本文介绍了如何从 rails 控制台获取详细的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我希望使用已经存在的电子邮件从控制台创建一个用户.如果我做 User.create(...)

Say I wish to create a user from console with an email which already exists. If I do User.create(...)

因为模型验证(本例中的唯一性).create 失败,rails 控制台的输出会说:rollback transaction (user is not saved)

Because of model validations (uniqueness in this case). create fails and the output in rails console will say: rollback transaction (user is not saved)

为了测试的目的.他们是我获得触发回滚的显式错误的一种方式吗?在这种情况下,它会说:回滚:电子邮件不是唯一的".

For the purpose of testing. Is their a way I can get the explicit error which triggered the rollback? In this case it would say something like: "ROLLBACK: email is not unique".

推荐答案

你也可以这样做:

User.create!

'bang' 将强制它向您显示错误,而无需其他步骤.

The 'bang' will force it to show you the errors without additional steps.

这篇关于如何从 rails 控制台获取详细的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆