Skip to content

Captcha

JP Barbosa edited this page Jul 23, 2015 · 2 revisions

Captcha

Install Rails Recaptcha Gem
echo "gem 'recaptcha', require: 'recaptcha/rails'" >> Gemfile
bundle install
Add initializer
nano config/initializers/recaptcha.rb
Recaptcha.configure do |config|
  config.public_key  = ENV['RECAPTCHA_PUBLIC_KEY']
  config.private_key = ENV['RECAPTCHA_PRIVATE_KEY']
  config.api_version = 'v2'
end
Add tags and controller check
nano app/views/recommendations/_form.html.erb
<%= simple_form_for([@article, @recommendation]) do |f| %>
  <%= f.input :email %>
  <%= recaptcha_tags %>
  <%= f.button :submit, class: 'btn btn-primary' %>
<% end %>
nano app/controllers/recommendations_controller.rb
...
if @recommendation.valid? && verify_recaptcha(model: @recomendation)...
        format.html { redirect_to articles_url, notice: 'Recommendation...' }
        format.json { render json: @recomendation }
else
...
Restart Rails server
rails s
Send recommendation using the browser
open http://localhost:3000/articles/1/recommendations/new
Add Recaptcha to Git
git add .
git commit -m "Add recaptcha"