Rails 3-表单提交人员检查之前 [英] Rails 3 - Before form submission human check

查看:44
本文介绍了Rails 3-表单提交人员检查之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的消息形式(:name,:title,:body),其中包含一个Recaptcha.当我在同一页面上同时拥有表单和Recaptcha时,我不会获得相同的用户体验,因为我的服务器端表单模型验证无法像我希望的那样工作.因此,我在考虑将表单属性保存到数据库之前,实现对单独的Recaptcha页的重定向.

I've got a simple message form (:name, :title, :body) that has a recaptcha. When I have both my form and recaptcha on the same page I don't get the same user experience because my server-side form model validations don't work like i'd like them to. So I was thinking of implementing a redirect to a seperate recaptcha page before the form attributes get saved into my database.

我想知道这种情况下的最佳做法是什么?是否应该将消息属性的会话发送给另一个处理Recaptcha的控制器?如果是这样的话,我会迷失在如何解决这个问题上.有什么想法吗?

I was wondering what are the best practices for this situation? Should I send a session of the message attributes to another controller that handles the recaptcha? If that's the case, I'm a bit lost on how to go about that. Any ideas?

推荐答案

另一种方法是创建一个附加的模型属性,即名称为submission_step的整数,其值对应于用户执行的操作步骤在提交过程中.在这种情况下,只有两个:

Another way to do it is to create an additional model attribute, an integer with a name like submission_step, with values corresponding to what step the user got to in the submission process. In this case there are only two:

0: before recaptcha
1: after successful recaptcha

因此,当用户首次提交表单时,将用0submission_step实例化一条新消息,然后运行验证,并假设它们通过了未经验证"的消息.然后,向用户发送recaptcha页面.如果他们成功完成了重新捕获,则记录将更新以将submission_step属性更改为1.

So when the user first submits the form, a new message is instantiated with a submission_step of 0, validations are run and assuming they pass the "uncaptcha'd" message is created. The user is then sent the recaptcha page. If they do the recaptcha successfully, the record is updated to change the submission_step attribute to a 1.

然后,您可以将submission_step用作合并范围,以过滤已通过recaptcha步骤的表单.您还可以使用validate中的:if:unless条件选项基于该步骤进行验证.

You can then use submission_step as a scope to filter for forms that have passed the recaptcha step. You can also validate based on the step using :if or :unless conditional options in validate.

当然,这会在数据库中为尚未通过recaptcha步骤的表单创建记录,这可能是一个问题-尽管您可以定期清除它们.但是我发现这是一个非常干净的解决方案,如果您以后想在此过程中添加其他步骤,可以轻松实现.

Of course this creates records in the database for forms that have not passed the recaptcha step, which might be a problem -- although you could periodically clean them out. But I've found it's a fairly clean solution and makes it easy if you want to add additional steps to the process in the future.

这篇关于Rails 3-表单提交人员检查之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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