Skip to content
This repository was archived by the owner on Aug 8, 2019. It is now read-only.

Using with Rails 4 and Devise

Anuj Dutta edited this page Dec 16, 2016 · 2 revisions

This wiki document describes the way to use is_bot with Devise and Rails 4. With Rails 4, a list of legitimate parameters need to be listed in the controller. As a result, we need to allow the captcha_in_reverse as a permitted field to the devise controller. Here's how you do it for sign up form in devise. captcha_in_reverse is the field added to your form to catch out any bots when you use is_bot gem.

Assumptions

  • You know HAML.
  • You also know how to extend devise default views/controllers.

## Steps

Controller

Create a registrations_controller like the one below

class RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters

  private

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up).push(:captcha_in_reverse)
  end

end

If you already have a registrations_controller, just add the following lines to it then:

  before_filter :configure_permitted_parameters

  private

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up).push(:captcha_in_reverse)
  end

Routes

Add the following line to your routes file to inform devise of the custom registrations controller:

devise_for :users, controllers: {
  registrations: "registrations"
}

Model

Add the following line to your user model:

class User < ActiveRecord::Base
   // Other code

   validate_captcha

   // Other methods
end

View

Finally, add the following line to your view file, most typically at app/views/devise/registrations/new.html.haml. This line should be added with rest of the field definitions:

= f.input :email
= f.input :password
= captcha_reverse_field resource.class.to_s.downcase

resource.class.to_s.downcase will be evaluated to :user in this case.

Clone this wiki locally