How can you put a recaptcha with devise controller in ROR

0

I have this code:

In app / views / devise / registrations I have the following code:

<div class="row">
    <div class="col-md-4 col-md-offset-4">

        <h2 class="text-center">Sign up</h2>
        <br/>

        <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
          <%= render 'shared/devisemes' %>

          <div class="form-group">
            <%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control" %>
          </div>

          <div class="form-group">
            <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %>
          </div>

          <div class="form-group">
            <%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control"  %>
          </div>
          <div class="form-group">
             <%= recaptcha_tags %>
          </div>
          <div class="actions">
            <%= f.submit "Sign up", class: "btn btn-primary"  %>
          </div>
        <% end %>
    </div>
</div>

In app / controllore / application_controller.rb I have this code:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname])
      devise_parameter_sanitizer.permit(:account_update, keys: [:fullname])
  end
end

In secret.yml I have the following:

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  #recaptcha_site_key: <%= ENV["RECAPTCHA_SITE_KEY"] %>
  #recaptcha_secret_key: <%= ENV["RECAPTCHA_SECRET_KEY"] %>

I have put the correct gem in the gemafile file, and I have read several examples but I do not know how to implement recaptcha when the devise parameter. Can someone help me?

    
asked by Diego 16.11.2017 в 21:43
source

1 answer

0

To implement the recaptcha you can install the recaptcha gem with

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

For more information about this gem visit this link .

In this page you can see more information on how to integrate it with devise.

Basically says that:

  • install the recaptcha gem
  • Add <%= recaptcha_tags %> to the way you want to add recaptcha with this and you'll get the recaptcha.
  • Include prepend_before_action in the controller action where you implement the recaptcha.

If you want to add it to the registry, the following code will help you.

class RegistrationsController < Devise::RegistrationsController
  prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.

  private
    def check_captcha
      unless verify_recaptcha
        self.resource = resource_class.new sign_up_params
        resource.validate # Look for any other validation errors besides Recaptcha
        respond_with_navigational(resource) { render :new }
      end 
    end
end
    
answered by 18.01.2018 в 19:05