我正在使用带有Humanizer gem的Rails 4来检查用户是否为人。 到现在为止还挺好。但是现在在一个特定的形式中,我需要在允许提交表单之前检查用户是否是人。
到目前为止,我已经使用Google搜索,但没有找到任何来源或想法来做这件事。
我做过这些事情:
对人性化答案字段作出反应的创建函数,因此在输入答案后,所有数据都会发送到控制器以检查答案是否正确。
我发送给控制器进行检查的数据是:人性化问题,人性化问题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
注意:到目前为止,我已经拥有了进行验证所需的一切。但我没有一个想法可以开始。
可能的解决方案:是否可以访问所有问题和答案所在的人工化区域设置?这是一个好方法还是坏方法?
谢谢!
答案 0 :(得分: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