私はAmbethia Recaptcha gemを使用しています。フォームが有効な場合はすべて正常に動作します。しかし、そうでない場合、私はReCaptchaをリセットする必要があります。ここで作成する方法であって、フォームが正しい場合RailsフォームでのRecaptchaのリセット
def create
@users = User.all
@user = User.create(user_params)
respond_to do |format|
if verify_recaptcha(model: @user, timeout: 300, message: "Problem with ReCAPTCHA, please try again.") && @user.save
session[:user_id] = @user.id
flash[:notice] = "Thank you for signing up!"
format.html
format.js {render js: "window.location = '#{root_path}';"}
format.json { render :show, status: :created, location: @user }
if Rails.env.production?
UserMailer.welcome_email(@user).deliver
end
else
flash[:alert] = "Sign Up Failed!"
format.html { redirect_back(fallback_location: root_path) }
format.js
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
あなたが見るには、reCAPTCHAのは動作しますが、パスワードが一致しないように、検証エラーがある場合、reCAPTCHAのは、すでに確認され表示されます。その後、検証エラーを修正すると、ReCaptchaをやり直すことができず、「ReCAPTCHA ...の問題」というエラーが表示されます。
<div class="modal-dialog">
<div class="spinner">
<%= image_tag "loading.gif" %>
</div>
<div class="modal-content">
<div id="registration-form">
<h1>Sign Up</h1>
<%= form_for @user, remote: true, html: { style: "display:inline;" } do |f| %>
<div class="modal-body">
<ul class="errors"></ul>
<div class="field">
<%= f.label :email %><br>
<%= f.email_field :email, required: true, autofocus: true, placeholder: "Enter a valid email" %>
</div>
<div class="field">
<%= f.label :username, 'Username' %><br>
<%= f.text_field :username, required: true, placeholder: "Pick a username" %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password, required: true, placeholder: "Create a password" %>
<p id="form-tip">Passwords must be at least <strong>8 characters</strong> in length and contain at least <strong>one upper case letter</strong>, <strong>one lower case letter</strong>, and <strong>one number or special character</strong>.</p>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation, required: true, placeholder: "Password again" %>
</div>
<%= render 'recaptcha' %>
<div class="modal-footer actions">
<%= f.submit "Sign Up", class: "btn btn-l btn-success"%>
<%= link_to "Cancel", "#", class: "btn btn-l btn-danger", data: {dismiss: "modal"} %>
</div>
<% end %>
</div>
</div>
</div>
</div>
この形式は、ナビゲーションバーのボタンからAJAX要求でレンダリングされます。これは私のrecaptcha_tags
を含むフォームです。私はreCAPTCHAのリロードを行うこと$(".g-recaptcha").grecaptcha.reset();
を追加しようとしました
$("ul.errors").html("")
<% if @user.errors.any? %>
<% @user.errors.full_messages.each do |message| %>
$("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
<% end %>
<% else %>
$("#new_user_modal").modal("hide")
<% end %>
:フォームの送信には、以下の_save.js.erbをトリガします。私はたくさんのことを試しました。何も動作していないようです。 フォームに検証エラーが再ロードされると、新しいReCAPTCHAボックスが表示されます。いずれか、または何らかの形でユーザーがエラーを修正し、再チャプターをやり直さずに続行できます。
おかげ