【问题标题】:Google RecaptchaV3 with rails Devise return a 405 error带有 Rails Devise 的 Google RecaptchaV3 返回 405 错误
【发布时间】:2021-09-16 14:26:33
【问题描述】:

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

在注册时,registrations_controller 中的“return if verify_recaptcha(action: 'signup')”行返回 405“Method Not Allowed”

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

【问题讨论】:

    标签: ruby-on-rails devise recaptcha


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-16
      • 2019-12-16
      • 2017-06-08
      • 2019-12-24
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多