DEV Community

Discussion on: Adding reCaptcha v3 to a Rails app without a gem

Collapse
 
storrence88 profile image
Steven Torrence

Did you run into any issues with Turbolinks doing it this way? I noticed the page with my form will only load the recaptcha if I refresh the page.

I tried wrapping the grecaptcha.ready function in a document.on(turbolinks:load) like this:

<script>
  $(document).on('turbolinks:load', function() {
      grecaptcha.ready(function() {
          grecaptcha.execute('#{ENV['RECAPTCHA_SITE_KEY']}', {action: '#{action}'}).then(function(token) {
            document.getElementById("#{id}").value = token;
          });
       });
   });
</script>
Enter fullscreen mode Exit fullscreen mode

but to no avail. Any suggestions?

Collapse
 
sammymhowe profile image
Samantha Howe

Hey did you ever figure this out? Running into Uncaught ReferenceError: grecaptcha is not defined and it's referencing a lot of Turbolinks links.

Collapse
 
storrence88 profile image
Steven Torrence

Hey! Sorry for the late reply. It's been awhile but I think I ended up disabling turbolinks for that particular page and the code ran fine after that.

Check this out: github.com/turbolinks/turbolinks#d...

Hope that helps!

Thread Thread
 
sammymhowe profile image
Samantha Howe

Ahh okay! I'll try that out, thank you so much!

Collapse
 
morinoko profile image
Felice Forby

Hey Steven, I did not have any issues with turbolinks doing it the way I wrote in the blog post. I unfortunately haven't tried it with a document.on(turbolinks:load) wrapper.