Search code examples
recaptcharuby-on-rails-7

Recaptcha gem needs a failed subimission on Rails7


A working form with recaptcha moved from rails6 to rails7 always arises a recaptcha validation error at first submit. On second submit it works.

  = form_with url: mail_submit_path do |form|
    = render partial: "err_details", locals: {field: :email}
    .field
      %label.label email
      .control.has-icons-left
        = form.text_field :email, value: @email, required: true, class: "input"
        %span.icon.is-left
          %i.fas.fa-envelope{:'aria-hidden' => "true"}
    = render partial: "err_details", locals: {field: :captcha}
    .field
      .recaptcha
        = recaptcha_tags
    .field
      .control
        = form.submit "Submit", class: "button is-link"

On first submit some data is missing: from the rails logs:

I, [2022-01-18T18:42:23.795409 #1451245]  INFO -- : [3c877c6c-5a2a-46e5-8ff6-5338c36d1078]   Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"[email protected]", "g-recaptcha-response"=>"                ", "commit"=>"Submit"}

I would think it is a turbo issue, but not sure.


Solution

  • I was having the same issue in Rails 6.1.4. For some reason, changing

    <%= recaptcha_tags %>
    

    to

    <%= recaptcha_tags(noscript: false) %>
    

    worked for me.

    I'd also like to add that I've had more success disabling Turbo for forms using recpatcha tags.

    <%= form_with(model: @resource, local: true, data: { turbo: "false"}) do |f| %>