Search code examples
formsdeviseregistrationhoneypot

Add a honeypot-field to Devise registration form


I will add a honeypot-field to the devise registration form (as a simple captcha). Where is the best place to check if data has been entered into this field?

If a bot has entered something into this field, he should be sent back to the index page without notification. As I am still far from being fluent in Rails, it would be helpful to see the syntax for this too.


Solution

  • I think the best place to do that is at the model, because it may be a business logic to accept only records from "verified humans".

    You can create a validation method that does this:

    class Comment < ActiveRecord::Base
      validate :honeypot_absence
    
      def honeypot_absence
        record.errors.add :invisible_field, "You should not fill the invisible field" unless invisible.field.blank?
      end
    end