Rails:创建时出现错误时如何处理数据
因此,我有一个订阅表单,用户可以同时创建一个新帐户并订阅每月计划。
因此,他们输入用户名、电子邮件、密码和所有常用的信用卡/账单信息。
但我遇到的问题是他们不小心输入了错误的信用卡号码......处理这个问题的最佳方法是什么?
我需要首先创建用户帐户,这样我就可以拥有一个数据库行 ID(用于第三方计费服务),但由于我已经在我的 create
方法中一次性处理了所有这些,如果用户更正其卡信息并重新提交,它告诉他们该帐户已经存在(因为用户帐户实际上已经创建)。
那么,这里的问题是你将如何处理这个问题?您会将他们带到仅包含帐单数据的错误页面吗?或者还有什么?不确定处理这个问题的最佳方法是什么......
So I have a subscription form where a user is simultaneously creating a new account plus subscribing to a monthly plan.
So they're entering username, email, password, and all the usual credit card/billing info.
But the problem I'm having is say they accidentally enter an incorrect credit card number...what's the best way to handle that?
I need to create the user account first so I can have a database row ID (for the third-party billing service), but since I have all of that handled in one swoop in my create
method, if the user corrects their card info and re-submits, it tells them the account already exists (since the user account was actually already created).
So, question here is how would you handle this? Would you take them to an error page that's only the billing data? Or what else? Not sure what the best way to handle this is...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道任何标准,但我确信最好的方法是在计费服务检查失败时删除新创建的用户帐户(字段
created_at
可能有助于检测超时)。这种机制是通过数据库事务实现的,并且在Rails中不需要太多努力。但请确保您没有在数据库上使用独占锁定。
Not aware of any standard, but I'm sure that the best way is to delete the newly created user account if billing service check failed (a field
created_at
may help to detect timeouts).This mechanism is implemented via database transactions and it doesn't take much effort in Rails. But make sure you're not using exclusive locking on your database.
在我看来,您可以通过两种不同的方式处理此问题:
1)在成功验证所有内容后保存数据。
您可以使用自定义验证来检查信用卡。
请参阅 snippets.dzone.com/posts/show/5266
2)使用两种不同的模型(用户和信用卡)并使用 find_or_create
(实际上您也可以将 find_or_create 与一种模型一起使用)
请参阅http://blogs.law.harvard.edu/lianaleahy/ 2008/05/19/find_or_create/
in my opinion, you could handle this in two different ways
1) just save the data after everything was successfully verified.
you can use a custom validation for checking the credit card.
see snippets.dzone.com/posts/show/5266
2) use two different models (users and credit_cards) and use find_or_create
(actually you could use find_or_create also with one model)
see http://blogs.law.harvard.edu/lianaleahy/2008/05/19/find_or_create/