【问题标题】:Humanizer gem human validation Rails 4 with Ajax, JqueryHumanizer gem 人工验证 Rails 4 与 Ajax,Jquery
【发布时间】:2016-09-28 16:06:14
【问题描述】:

我正在使用带有 Humanizer gem 的 Rails 4 来检查用户是否是人类。 到目前为止,一切都很好。但现在在一种特定的表单中,我需要在允许提交表单之前检查用户是否是人类。

到目前为止,我已经用谷歌搜索,但没有找到任何来源或想法来做这样的事情。

我做过这些事情:

创建了对人性化答案字段做出反应的函数,因此在输入答案后,所有数据都会发送到控制器以检查答案是否正确。

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

我的表格:

   <%= 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 

注意:到目前为止,我已经拥有了进行验证所需的一切。但我没有一个想法可以开始。

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

谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails ajax


    【解决方案1】:

    你可以试试这个,在你的模型中使用人性化方法

    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
    

    【讨论】:

    • 桑德罗·d。谢谢,完美运行。
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多