首页 > 解决方案 > Gem Recaptcha 我应该在哪里指定站点密钥?

问题描述

我正在尝试将 gem recaptcha 添加到我的应用程序中,但出现此错误No site key specified。堆栈溢出解决我的没有答案。

这是我在 gem 文件中的代码

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 ,:required => true ,placeholder: "Email"%>

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

有问题的控制器

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

根文件中的recaptch.env

export RECAPTCHA_SITE_KEY= xxxxxxxxxx
export RECAPTCHA_SECRET_KEY= xxxxxxxxx

标签: ruby-on-railsrecaptcha

解决方案


配置/初始化程序/recaptcha.rb:

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

推荐阅读