在 ruby​​ on Rails 中,什么代码会询问表单是否通过了所有验证?

发布于 2024-12-12 03:20:29 字数 399 浏览 0 评论 0原文

我想让我的表单转发到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷港 2024-12-19 03:20:29

仔细阅读基于表单的网站身份验证的权威指南 并问问自己是否真的需要验证码。

除此之外,您还可以使用 回调after_validationbefore_savearound_saveafter_savebefore_createaround_createafter_createbefore_updatearound_updateafter_update 处理仍在您内部的内容交易。

调用这些回调之一的方法是简单地在您的 model 中声明它们

,但是如果您需要使用验证码,我会使用 javascript 和 ajax 来完成此操作,以便在用户之前将其附加到您的表单中发送它。

收到表单帖子后,您不应该在控制器中执行此操作,因为您必须:

  1. 验证后将填写的表单值存储在会话中(不保存)
  2. 将用户重定向到验证码页面(这将使任何用户感到困惑) )
  3. 在通过之前多次检查验证码(它们非常不可读)
  4. 将模型从会话中取出(您不知道它是哪一个)
  5. 在模型上调用 save 以实际将其写入数据库。

所以基本上你可以避免在验证码通过之前开始交易。

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:

  1. Store the filled form values in the session after validation (dont save)
  2. Redirect the user to a captcha page (which will make any user confused)
  3. Check the captcha multiple times before it passes (they are quite unreadable)
  4. Get the model out of the session (which you have no idea of which one it is)
  5. Call save on the model to actually write it to your DB.

So basically you avoid starting a transaction before the captcha is passed.

李不 2024-12-19 03:20:29

验证存在于模型中,您可以简单地在控制器中执行此操作:

@user.valid?

然后执行您的验证码操作。

另一个解决方案是使用回调,例如:before_save 或before_create ,但前提是可以在模型中访问recaptcha(我对此表示怀疑)。

Validation lives in the model, you could simply do this in the controller:

@user.valid?

and then do your recaptcha stuff.

Another solution is to use callbacks such as: before_save or before_create but only if recaptcha could be accessed in model (which I doubt).

旧情勿念 2024-12-19 03:20:29

此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文