Я пытаюсь добавить защиту от спама в мою регистрационную форму Devise, используя acts_as_textcaptcha , но по какой-то причине она не отображается в моей форме.
На основе Как: использовать Recaptcha с Devise . Блог My Rails 6 доступен здесь для клонирования: https://github.com/anonymous-donor/blog-with-textcaptcha
Спасибо!
Gemfile
gem 'acts_as_textcaptcha', '~> 4.5'
models / user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
acts_as_textcaptcha
end
controllers / 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 / rout.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'