Module: Controllers::TurnstileVerification

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/concerns/controllers/turnstile_verification.rb

Overview

Cloudflare Turnstile CAPTCHA integration

Provides helper methods for rendering and validating Turnstile widgets.

Usage:

In view - render widget

<%= turnstile_widget %>
<%= turnstile_lazy_widget %> # Lazy-loaded version

In controller action - validate

validate_turnstile!

Instance Method Summary collapse

Instance Method Details

#load_turnstile_script_tagString

Load Turnstile script tag into the head (only once)

Returns:

  • (String)

    empty string once the script is queued in content_for :head
    (falls back to the script tag itself when content_for is unavailable)



37
38
39
40
41
42
43
44
45
46
47
# File 'app/concerns/controllers/turnstile_verification.rb', line 37

def load_turnstile_script_tag
  return ''.html_safe unless Turnstile.enabled?
  return ''.html_safe if content_for?(:turnstile_script_loaded)

  content_for :turnstile_script_loaded, true
  content_for :head, turnstile_script_tag
  ''.html_safe
rescue NoMethodError
  # Fallback if content_for is not available (e.g., in tests or console)
  turnstile_script_tag
end

#turnstile_lazy_widget(**options) ⇒ String

Lazy-loaded Turnstile widget - only loads script when visible in viewport
Use this instead of turnstile_widget + load_turnstile_script_tag for better performance

Parameters:

  • options (Hash)

    widget attributes

Options Hash (**options):

  • theme (String)

    widget theme: "light" (default), "dark", or "auto"

  • size (String)

    widget size: "normal" (default), "flexible", or "compact"

  • root_margin (String)

    IntersectionObserver margin before the
    script loads (default: "200px")

  • class (String)

    additional CSS classes for the container

Returns:

  • (String)

    HTML for the widget (empty string when Turnstile is disabled)



72
73
74
75
76
# File 'app/concerns/controllers/turnstile_verification.rb', line 72

def turnstile_lazy_widget(**options)
  return ''.html_safe unless Turnstile.enabled?

  Turnstile.lazy_widget(options)
end

#turnstile_script_tagString

Render the Turnstile script tag

Returns:

  • (String)

    HTML for the script tag (empty string when Turnstile is disabled)



26
27
28
29
30
31
# File 'app/concerns/controllers/turnstile_verification.rb', line 26

def turnstile_script_tag
  return ''.html_safe unless Turnstile.enabled?

  nonce = request&.content_security_policy_nonce
  Turnstile.script_tag(nonce: nonce)
end

#turnstile_widget(**options) ⇒ String

Render the Turnstile widget

Parameters:

  • options (Hash)

    widget attributes; every key is rendered as a
    data-<key> attribute on the widget div (see Cloudflare Turnstile docs)

Options Hash (**options):

  • theme (String)

    widget theme: "light", "dark", or "auto"

  • size (String)

    widget size: "normal", "flexible", or "compact"

Returns:

  • (String)

    HTML for the widget (empty string when Turnstile is disabled)



56
57
58
59
60
# File 'app/concerns/controllers/turnstile_verification.rb', line 56

def turnstile_widget(**options)
  return ''.html_safe unless Turnstile.enabled?

  Turnstile.widget(options)
end

#validate_turnstile!Boolean?

Validate Turnstile response and redirect with error if failed
Call this from create/update actions that require CAPTCHA validation

Returns:

  • (Boolean, nil)

    true when validation passes or is skipped; otherwise
    redirects back with an error flash and returns nil



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/concerns/controllers/turnstile_verification.rb', line 83

def validate_turnstile!
  # Skip entirely in development/test - production keys don't work on localhost,
  # and system tests (Playwright) cannot solve a CAPTCHA challenge.
  return true if Rails.env.local?

  return true unless Turnstile.enabled?

  token = params['cf-turnstile-response'] || params['cf_turnstile_response']
  ok = Turnstile.verify(token, remote_ip: request.remote_ip)

  return if ok

  log_turnstile_failure(token)
  store_failed_form_data
  flash[:error] = 'Please complete the security verification to submit the form.'
  redirect_back_or_to(root_path)
end