在 ruby on Rails 中,什么代码会询问表单是否通过了所有验证?
我想让我的表单转发到 recaptcha,但只有在表单通过所有验证之后。在将用户详细信息保存到数据库之前,我该如何实现这一点?
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
#to recaptcha, but before save and only after sign up form passes validation
else
format.html { render :new }
format.js { render :form_errors }
end
end
end
I want to have my form forward to recaptcha but only after form has passed all validation. How would I achieve this before users details are saved to DB?
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
#to recaptcha, but before save and only after sign up form passes validation
else
format.html { render :new }
format.js { render :form_errors }
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仔细阅读基于表单的网站身份验证的权威指南 并问问自己是否真的需要验证码。
除此之外,您还可以使用 回调 :
after_validation
、before_save
、around_save
、after_save
、before_create
、around_create
、after_create
、before_update
、around_update
、after_update
处理仍在您内部的内容交易。调用这些回调之一的方法是简单地在您的
model
中声明它们,但是如果您需要使用验证码,我会使用 javascript 和 ajax 来完成此操作,以便在用户之前将其附加到您的表单中发送它。
收到表单帖子后,您不应该在控制器中执行此操作,因为您必须:
所以基本上你可以避免在验证码通过之前开始交易。
Have a good look at The definitive guide to form-based website authentication and ask yourself if you really need a captcha.
Besides that, you can use Callbacks :
after_validation
,before_save
,around_save
,after_save
,before_create
,around_create
,after_create
,before_update
,around_update
,after_update
to handle stuff still inside your transaction.The way to call one of these callbacks is to simply declare them in your
model
If you need to use a captcha however, I would do this with javascript and ajax, to append it to your form before the user sends it.
You should not do this in the controller after recieving a post of the form, since you will have to:
So basically you avoid starting a transaction before the captcha is passed.
验证存在于模型中,您可以简单地在控制器中执行此操作:
然后执行您的验证码操作。
另一个解决方案是使用回调,例如:before_save 或before_create ,但前提是可以在模型中访问recaptcha(我对此表示怀疑)。
Validation lives in the model, you could simply do this in the controller:
and then do your recaptcha stuff.
Another solution is to use callbacks such as:
before_save
orbefore_create
but only if recaptcha could be accessed in model (which I doubt).此 Railscast 包含您需要了解的有关多步骤表单的所有信息。这一集涵盖了验证以及步骤之间的来回移动。 http://railscasts.com/episodes/217-multistep-forms
这听起来像你的表单有两个步骤,第一步是输入所有信息,第二步只是输入验证码。
现在,在我看来,您应该将验证码滚动到主用户输入表单中,并将其全部保留在一个页面中,而不是进行两步过程,我之前已经完成了这两步,并且让验证码成为同一表单的一部分是非常非常容易且不那么复杂。将所有内容都放在一个表单中可以让您将所有逻辑(大部分)合并到单个控制器操作中。您可能可以将控制器中的逻辑抽象为辅助方法,例如验证码的验证,这将使您的控制器操作变得不那么复杂。您最不想做的就是使您的操作逻辑过于复杂。
This Railscast has all you need to know about multistep forms. The episode covers validation and moving back and forth between steps. http://railscasts.com/episodes/217-multistep-forms
It sounds like your form has two steps, the first being where they enter in all their information, and the second being just a captcha entry.
Now, in my opinion you should just roll the captcha into the main user entry form and keep it all to a single page rather than having a two step process, I've done both before and having the captcha be part of the same form is much, much easier and less complex. Having everything in a single form allows you to have all of your logic consolidated (mostly) into a single controller action. There may be logic you can abstract out of the controller into a helper method, like the verification of the captcha, which will make your controller action that much less complicated. The last thing you want to do is over-complicate your action logic.