Robert B
Robert B

Reputation: 2883

Setting Up Devise with Recaptcha - Rails 3

I have tried setting this up based on previously asked questionshere on stack overflow but haven't been able to get it working. The captcha is showing up on my forms but a user can still register without filling in the recaptcha.

I'm working with the following instructions. https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise

  1. I've got my private and public recaptcha keys

Environment.rb

ENV['RECAPTCHA_PUBLIC_KEY']  = 'mykey1234567'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey1234567'
  1. I've installed the recaptcha gem.

Gemfile

source 'http://rubygems.org'

gem 'rails', '3.0.3'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'devise', '1.1.7'
gem "jquery-rails"
gem 'recaptcha', :require => 'recaptcha/rails'
  1. I've added recaptcha tags to my registration view.

new.html.erb

      <%= recaptcha_tags %>

  <p><%= f.submit "Sign up" %></p>
  1. Created Registrations Controller

    rails generate controller Registrations create

Registrations Controller

class RegistrationsController < Devise::RegistrationsController

def create
      if verify_recaptcha
        super
      else
        build_resource
        clean_up_passwords(resource)
        flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code and click submit."
        render_with_scope :new
      end
    end
end

I am supposed to edit my routes file but not sure what exactly which may be the cause of the issue.

My Routes File

  devise_for :troopers, :path => "troopers", :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

Thanks for any help.

Upvotes: 2

Views: 2040

Answers (1)

mbreining
mbreining

Reputation: 7809

You also need to tell Devise to use your customized RegistrationsController. You do this by specifying the :controllers options in your devise_for declaration. Without this the Devise::RegistrationsController is called, which probably explains why recaptcha isn't working.

  devise_for :troopers,
             :controllers => { :registrations => "registrations" },
             :path => "troopers",
             :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

Upvotes: 2

Related Questions