Humanizer gem人工验证与Aj​​ax,Jquery的Rails 4 [英] Humanizer gem human validation Rails 4 with Ajax, Jquery

查看:108
本文介绍了Humanizer gem人工验证与Aj​​ax,Jquery的Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rails 4与Humanizer配合使用,以检查用户是否是人类. 到目前为止,一切都很好.但是现在以一种特定的形式,我需要在允许提交表单之前检查用户是否为人.

I am using Rails 4 with Humanizer gem that checks if user is human. So far so good. But now in one specific form I need to check if user is human before allowing to submit form.

到目前为止,我已经在Google上进行了搜索,但没有找到任何来源或想法来做这种事情.

So far I have googled but didn't find any source or idea to do such thing.

我已完成以下操作:

创建的函数可对人性化答案字段进行反应,因此在键入答案后,所有数据都会发送到控制器以检查该答案是否正确.

Created function that reacts on humanizer answer field, so after typing answer all data is sent to controller to check if that answer is correct.

我要发送给控制器进行检查的数据是:人性化问题,人性化问题ID,人性化答案.

Data that I am sending to controller for checking is: humanizer question, humanizer question id, humanizer answer.

我的表单:

   <%= form_for(@advertisement,:html => {:id=>"new_advertisement","data-parsley-validate" => true,:multipart => true}) do |f| %>


     <%= f.label :humanizer_answer, @advertisement.humanizer_question %>     
     <%= f.hidden_field :humanizer_question_id ,:id=> "humanizer_question_id"%>
     <%= f.hidden_field :humanizer_question , :value=>@advertisement.humanizer_question, :id=> "humanizer_question"%>                  
     <%= f.text_field :humanizer_answer, :'data-validate_humanizer' => '/blocked/checkhumanizer',:'data-parsley-conditionalrequired'=>'["[name=\"paid\"]:checked", "false"]',:'data-parsley-validate-if-empty data-parsley-success-class' =>""%>  

 <button type="submit" class="blue-button btn btn-default" style="width:380px !important;"><%= t('add') %></button>

脚本:

$( '[data-validate_humanizer]').blur(function() {
        $this = $(this);
        $.get($this.data('validate_humanizer'), {
            question_id: $('#humanizer_question_id').val(),
            question: $('#humanizer_question').val(),
            answer: $(this).val()

        }).success(function() {

            alert("sucess");

        }).error(function() {

            alert("Error");

        });
});

控制器:

def checkhumanizer
       if params[:question_id].present? && params[:question].present? && params[:answer].present?

          render :nothing => true, :status => 200
       else
           render :nothing => true, :status => 404
       end

   end

路线:

 resources :blocked do
        collection do
           get 'checkhumanizer'
        end
end 

注意:到目前为止,我已经具备了进行验证所需的一切.但是,我并没有一个主意.

Note: So far I have everything I should need to make a validation.But I have not a single idea to start with.

可能的解决方案:是否可以访问所有问题和答案都位于的人性化语言环境?是好方法还是坏方法?

Possible solution: Is it possible to access humanizer locales where all questions and answers are located ? Is it a good approach or bad?

谢谢!

推荐答案

您可以尝试使用模型中的人性化方法

You can try this, using the humanizer methods in your model

def checkhumanizer
  if params[:question_id].present? && params[:answer].present?

    ad = Advertisement.new
    ad.humanizer_question_id = params[:question_id]
    ad.humanizer_answer = params[:answer]

    if ad.humanizer_correct_answer?
      render :nothing => true, :status => 200
    else
      render :nothing => true, :status => 404
    end

  else
    render :nothing => true, :status => 404
  end

end

这篇关于Humanizer gem人工验证与Aj​​ax,Jquery的Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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