【发布时间】: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