Google RecaptchaV3 rails 设计 return 405 错误

Google RecaptchaV3 with rails Devise return a 405 error

我想安装 google recaptchaV3。我一步一步地遵循了这个指南 https://github.com/heartcombo/devise/wiki/How-To:-Use-Recaptcha-with-Devise.

在sign_up上,行“return if verify_recaptcha(action: 'signup')”在registrations_controller return 405“方法不允许

recaptcha.rb

Recaptcha.configure do |config|
  config.site_key = Rails.application.credentials.dig(:google, :RECAPTCHA_PUBLIC_KEY)
  config.secret_key = Rails.application.credentials.dig(:google, :RECAPTCHA_PRIVATE_KEY)
  config.proxy = "http://www.google.com/recaptcha/api/verify"
end

routes.rb

Rails.application.routes.draw do

  devise_for :users, controllers: {
    registrations: 'registrations',
    omniauth_callbacks: 'users/omniauth_callbacks',
    passwords: 'users/passwords'
  }
end

devise/registrations/new.html.erb

  <%= simple_form_for(resource, html: { class: "mt-8 space-y-4" },
                      as: resource_name,
                      url: registration_path(resource_name)) do |f| %>
    <%= f.error_notification %>
    <%= flash[:recaptcha_error] %>
    <%= recaptcha_v3(action: 'signup') %>
    <%= f.input :email %>
    <%= f.input :pseudo %>
    <%= f.input :password %>
    <%= f.input :password_confirmation %>
    <%= f.button :submit %>
  <% end %>

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController #:nodoc:
  prepend_before_action :check_captcha, only: [:create]

  private

  def sign_up_params
    params.require(:user).permit(:pseudo, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:pseudo, :email, :password, :password_confirmation, :current_password)
  end

  def after_update_path_for(resource)
    user_path(resource)
  end

  private

  def check_captcha
    return if verify_recaptcha(action: 'signup')

    self.resource = resource_class.new sign_up_params
    resource.validate # Look for any other validation errors besides reCAPTCHA
    set_minimum_password_length
  
    respond_with_navigational(resource) do
      flash.discard(:recaptcha_error) # We need to discard flash to avoid showing it on the next page reload
      render :new
    end
  end
end

只需删除:config.proxy = "http://www.google.com/recaptcha/api/verify" 在 recaptcha.rb