我使用Rails 4和人机界面gem来检查用户是否是人。到目前一切尚好。但是现在在一种特定的表单中,我需要在允许提交表单之前检查用户是否是人。
到目前为止,我已经搜索过,但没有找到任何来源或想法来做这样的事情。
我做了以下几件事:
创建了响应于人性化应答字段的函数,因此在输入应答后,所有数据都被发送到控制器,以检查该答案是否正确。
我发送给控制器检查的数据是:人性化的问题,人性化的问题id,人性化的答案。
My form:
<%= 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:到目前为止,我已经具备了制作validation.But所需的一切,我一开始没有一个想法。
可能的解决方案:是否可以访问所有问题和答案所在的人性化环境?这是好方法还是坏方法?
谢谢!
发布于 2016-09-28 17:59:57
您可以在您的模型中使用人性化方法来尝试这一点。
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
endhttps://stackoverflow.com/questions/39752565
复制相似问题