matthutchinson / acts_as_textcaptcha

Text-based logic question captcha's for Rails šŸš«šŸ¤–

Home Page:https://acts-as-textcaptcha.hiddenloop.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why are the answers always incorrect?

hugalves opened this issue Ā· comments

I really don't understand why the answers are always incorrect.

app/controllers/landing_controller.rb

class LandingController < ApplicationController
 def index
   @contact  = Contact.new
   @contact.textcaptcha
 end

app/models/contact.rb

class Contact < ActiveRecord::Base
  attr_accessible :name, :email, :message, :contact_type, :status
  validates :name, :email, :contact_type, :message, :status, :presence => true
  validates :message, :length => { :minimum => 15, :maximum => 100 }

  acts_as_textcaptcha  :http_read_timeout    => 60,
                  :http_read_timeout    => 10,
                  :cache_expiry_minutes => 10,
                  :questions            => [{ 'question' => 'Quanto Ć© 1 + 1?', 'answers' => '2,dois' },
                                            { 'question' => 'Qual a cor do chapƩu verde?', 'answers' => 'verde' }]

app/views/landing/index.html.erb

<form id="contact" method="post" action="contact" accept-charset="UTF-8">
  <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>"/>
  <h2>ENTRE EM CONTATO</h2>
  <div>
    <label>Nome</label>
    <input type="text" placeholder="Nome" id="contact[name]" name="contact[name]" />
  </div>
  <div>
    <label>E-mail</label>
    <input type="text" placeholder="E-mail" id="contact[email]" name="contact[email]" />
  </div>
  <div>
    <label>Tipo</label>
    <select name="contact[contact_type]" id="contact[contact_type]">
      <option value="1">SugestĆ£o</option>
      <option value="2">ReclamaĆ§Ć£o</option>
      <option value="3">Elogio</option>
      <option value="4">Comercial</option>
    </select>
  </div>
  <div>
    <label>Mensagem</label>
    <textarea placeholder="Mensagem" id="contact[message]" name="contact[message]"></textarea>
  </div>
  <div>
    <label for="contact_textcaptcha_answer"><%= @contact.textcaptcha_question %></label>
    <input id="contact_textcaptcha_answer" placeholder="<%= @contact.textcaptcha_question %>" name="contact[textcaptcha_answer]" type="text" value="" />
  </div>
  <input id="contact_textcaptcha_key" name="contact[textcaptcha_key]" type="hidden" value="xxxxxxxxxxx" />
  <input type="submit" class="lnk blue fR" value="ENVIAR">
</form>

So, the questions are shown correctly. When submiting I'm receiving:

Started POST "/contact" for 127.0.0.1 at 2014-03-27 23:02:40 -0300
Processing by LandingController#contact as HTML
Parameters:      {"authenticity_token"=>"I9+5hvM620qpifeKGB3fez7JuAU+V72EViOoE6UhfHE=", "contact"=>{"name"=>"[FILTERED]", "email"=>"[FILTERED]", "contact_type"=>"1", "message"=>"asdasdsadasdasdads", "textcaptcha_answer"=>"frio", "textcaptcha_key"=>"xxxxxx"}}
(0.2ms)  BEGIN
(0.2ms)  ROLLBACK
Could not save contact. Exception: Textcaptcha answer was not submitted quickly   enough, try another question instead
Redirected to http://localhost:3000/
Completed 302 Found in 2472ms (ActiveRecord: 0.4ms)

Where am I having a mistake?

OK, i've taken a look and checked everything with the exact same settings and code you have above, and it works OK for me. This makes me think that problem you are having is down to not having good Rails.cache set. You can check that the answers are being set in the cache by doing the following;

Refresh your form, view page source and grab the contact[textcaptcha_key] value. Then open a Rails console and try;

# where ??? is the value of  the textcaptcha_key
ActsAsTextcaptcha::TextcaptchaCache.new.read('?????')

# also can you tell me what this gives you?
Rails.cache.class.to_s

Since v4, acts_as_textcaptcha requires a working Rails.cache store to be configured. The cache is used to persist answers to check against, the cache key changes every time you submit the form. By default answers live in the cache for DEFAULT_CACHE_EXPIRY_MINUTES (10 minutes).

And in your config above you've specified this with the : cache_expiry_minutes option. So everything should be fine, unless something is up with your cache. (Maybe check that some other process or action isn't clearing it with Rails.cache.clear)

Let me know how you get on...

Closing this now, since its an old issue and I could not repeat.. If you're still having trouble, please upgrade to the latest version of the gem and try again.