Gem Recaptcha我应该在哪里指定站点密钥?

问题描述

我正在尝试将gem Recaptcha添加到我的应用程序,但是出现此错误function Provider<T extends { new(...args: any[]): {} }>( konstructor: T ) { const klass = class extends konstructor { constructor(...args: any[]) { super(args) Injector.register('scope',this); } } return klass; } 。堆栈溢出解决我的问题没有答案。

这是我的密码 在gem文件

No site key specified

以新表格的形式

gem "recaptcha",require: "recaptcha/rails"

问题控制器

<%= form_for @question do |f| %>
          <div class="row">
            <div class="col-md-6 col-sm-6 col-xs-6">
              <%= f.text_field :first_name,:required => true,placeholder: "First Name"%>
            </div>
            <div class="col-md-6 col-sm-6 col-xs-6">
              <%= f.text_field :last_name,placeholder: "Last Name" %>
            </div>
          </div>
          
            <%= f.text_field :email,placeholder: "Email"%>

            <%= f.text_area :description,placeholder: "Questions or Comments",rows: "4",cols: "50"%><br>
            <%= recaptcha_tags %>
            <%= f.submit "SEND My Message",class: "btn btn-danger" %>
          <% end %>

root文件中的recaptch.env

class QuestionsController < ApplicationController
  before_action :find_question,only: [:destroy]

  def index
    @questions = Question.order("updated_at DESC")
  end

  def create
    @question = Question.new(question_attributes)
    if verify_recaptcha(model: @user) && @question.save
      ContactMailer.message_send(@question).deliver
      redirect_to new_question_path,notice: "Thank you... Your message was created successfully."
    else
      flash.Now[:error] = "Please correct the form"
      render :new
    end
  end

  def show
    @question = Question.find(params[:id])
  end

  def new
    @question=Question.new 
  end

  def destroy
    if @question.destroy
      redirect_to questions_path,notice: "Message deleted successfully."
    else
      redirect_to question_path,error: "We had trouble deleting."
    end
  end

  private

  def question_attributes
    question_attributes = params.require(:question).permit([:first_name,:last_name,:email,:description])
  end

   def find_question
    @question = Question.find(params[:id])
  end

end

解决方法

config / initializers / recaptcha.rb:

Recaptcha.configure do |config|
  config.site_key = XXXX
  config.secret_key = XXX
end