gpt4 book ai didi

ruby-on-rails - Google Recaptcha with rails Devise

转载 作者:行者123 更新时间:2023-12-04 05:33:21 29 4
gpt4 key购买 nike

我正在使用 devise 将 google recaptcha 添加到我的 rails 应用程序中。我做了以下事情:

#In my gemfile:
gem "recaptcha", require: "recaptcha/rails"

在我的初始化器中:

#In config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.site_key = '********************'
config.secret_key = '********************'
# Uncomment the following line if you are using a proxy server:
# config.proxy = 'http://myproxy.com.au:8080'
end

在 application.html.erb 布局文件中:

<script src='https://www.google.com/recaptcha/api.js'></script>

在设计/注册/new.html.erb

  <%= flash[:recaptcha_error] %>
<%= recaptcha_tags %>

注册 Controller

class RegistrationsController < Devise::RegistrationsController
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.
before_action :configure_permitted_parameters, :only => [:create]


protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :remember_me,:username, :name, :country, :sex, :age_group, :race) }
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:email, :password, :remember_me,:username, :name, :country, :sex, :age_group, :race) }
end

private
def check_captcha
unless verify_recaptcha
self.resource = resource_class.new configure_permitted_parameters
#resource.validate # Look for any other validation errors besides Recaptcha
respond_with_navigational(resource) { render :new }
end
end
end

当我注册时,页面被重定向到我的注册页面本身。在 recaptcha 元素上显示 recaptcha 错误消息。服务器日志显示:

Filter chain halted as :check_captcha rendered or redirected

Started POST "/users" for 127.0.0.1 at 2018-01-23 13:01:10 +0530
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dv6+vEH1KlMbrqssbX1nW9PbDYtmu+92QAZVvGO5JedpmHj8S8Jr4vuvbUUuBwWQO6MK04daOqzCZyOPXMvnLw==", "user"=>{"username"=>"ssfafa", "name"=>"afafaf", "country"=>"AI", "age_group"=>"dffssaf", "sex"=>"Male", "race"=>"sfsfsf", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Rendering devise/registrations/new.html.erb within layouts/application
Rendered devise/shared/_links.html.erb (2.0ms)
Rendered devise/registrations/new.html.erb within layouts/application (17.5ms)
Filter chain halted as :check_captcha rendered or redirected
Completed 200 OK in 96ms (Views: 94.5ms | ActiveRecord: 0.0ms)

用户未添加到数据库。无法完成注册。请解决这个问题。提前致谢。

最佳答案

不知道为什么这不起作用,如果您的配置正常然后也不起作用然后尝试注释掉 check_captcha 方法然后重写 create 方法,如下所示

def create
if verify_recaptcha
super
else
flash.now[:error] = "Recaptcha cannot be blank; please try again"
render :new
end
end

和路线

devise_for :users, controllers: { registrations: 'registrations' }

希望对你有帮助

关于ruby-on-rails - Google Recaptcha with rails Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396517/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com