Google RecaptchaV3 with rails设计返回405错误



我想安装谷歌recaptchaV3。我按照这个指南一步一步地https://github.com/heartcombo/devise/wiki/How-To:-Use-Recaptcha-with-Devise.

在sign_up上,return if verify_recaptcha(action: 'signup')"在registrations_controller返回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

设计/注册/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

最新更新