LoginSignup
5
1

More than 5 years have passed since last update.

Install reCAPTCHA on Rails Application

Last updated at Posted at 2016-06-29

How to

1 - Create reCAPTCHA account.

2 - Add gem 'recaptcha'

# Gemfile
gem "recaptcha", require: "recaptcha/rails"

# DON'T omit "require" option like this!  'gem "recaptcha"'
# This cause the error such as:
# "undefined local variable or method `recaptcha_tags' " 

3 - Add validation to controller

# app/controllers/some_controller.rb

def create
    @user = Some.new user_params
    if verify_recaptcha(model: @some) && @some.save
      redirect_to account_login_path, notice: 'Success!'
    else
      render :new
    end
end

4 - Add reCAPTCHA tag in view file (in this case, edit haml file)

# app/view/some/new.html.haml
= form_for user, url: path do |f|
  = recaptcha_tags # ADDED

5 - Add reCAPTCHA config file

# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
  config.site_key  = 'Your Public key'
  config.secret_key = 'Your Private key'
  # Uncomment the following line if you are using a proxy server:
  # config.proxy = 'http://myproxy.com.au:8080'
end

6 - Bundle install and start server

bundle install
rails s

7 - Error massage translation

# config/locale/ja.yml
ja:
  recaptcha:
    errors:
      verification_failed: "認証に失敗しました"
      recaptcha_unreachable: "接続に失敗しました"

It is very easy way to integrate reCAPTCHA to your Rails Application.
Should Try!
Feel free to comment or ask Question.

  • 2017/02/14 fixed chapter "5 - Add reCAPTCHA config file". Thanks to @besukosan
5
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1