【问题标题】:Rails and Devise: Adding spam protection with acts_as_textcaptchaRails 和 Devise:使用acts_as_textcaptcha 添加垃圾邮件保护
【发布时间】:2020-05-12 09:44:51
【问题描述】:

我正在尝试使用 acts_as_textcaptcha 在我的 Devise 注册表单中添加垃圾邮件保护,但由于某种原因它不会显示在我的表单中。

基于How To: Use Recaptcha with Devise。我的 Rails 6 博客应用程序可在此处克隆:https://github.com/anonymous-donor/blog-with-textcaptcha

谢谢!

宝石文件

gem 'acts_as_textcaptcha', '~> 4.5'

models/user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

  acts_as_textcaptcha
end

控制器/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def new
    @user = User.new
    @user.textcaptcha
  end

  private
    # https://kakimotonline.com/2014/03/30/extending-devise-registrations-controller/
    def sign_up_params
      allow = [:email, :display_name, :password, :password_confirmation, :textcaptcha]
      params.require(resource_name).permit(allow)
    end
end

config/routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: {  registrations: "registrations" }
...
end

app/views/devise/registrations/new.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
...
  <%= textcaptcha_fields(f) do %>
    <div class="field">
      <%= f.label :textcaptcha_answer, @user.textcaptcha_question %><br/>
      <%= f.text_field :textcaptcha_answer, :value => '' %>
    </div>
  <% end %>
...
<% end %>

config/textcaptcha.yml

development:
  questions:
    - question: 'Is ice hot or cold?'
      answers: 'cold'
    - question: 'what color is an orange?'
      answers: 'orange'
    - question: 'what is two plus 3?'
      answers: '5,five'
    - question: 'what is 5 times two?'
      answers: '10,ten'
    - question: 'How many colors in the list, green, brown, foot and blue?'
      answers: '3,three'
    - question: 'what is Georges name?'
      answers: 'george'
    - question: '11 minus 1?'
      answers: '10,ten'
    - question: 'is boiling water hot or cold?'
      answers: 'hot'
    - question: 'what color is my blue shirt today?'
      answers: 'blue'
    - question: 'what is 16 plus 4?'
      answers: '20,twenty'


【问题讨论】:

    标签: ruby-on-rails ruby devise captcha


    【解决方案1】:

    对不起,如果我错了,你能说这是gem 'acts_as_textcaptcha', '~&gt; 4.5'吗?我在您提供的链接中找不到这个:https://github.com/ambethia/recaptcha。如果有任何问题,请关注该 gem 并回复。您可以参考同一页面来配置设计:https://github.com/heartcombo/devise/wiki/How-To:-Use-Recaptcha-with-Devise。谢谢。

    【讨论】:

    • 你在说什么?
    【解决方案2】:

    原来我必须在 config/environments/development.rb 中将 config.cache_store = :null_store 设置为 config.cache_store = :memory_store 才能在开发模式下工作。

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2021-01-26
      相关资源
      最近更新 更多