0

I am using Ruby on Rails.

create.js.erb

$('#my_re_ap').html('<%= recaptcha_element('signup')%>');

but I am getting a Uncaught SyntaxError: '' string literal contains an unescaped line break error.

My view helper looks like this. I am using the recaptcha gem.

helper

  def recaptcha_element(event_name)
    if @show_checkbox_recaptcha
      content_tag :div, class: "pb-2" do
        recaptcha_tags
      end
    else
      recaptcha_v3(action: event_name, site_key: 'key123')
    end
  end

What is wrong with my syntax?

Lefty
  • 535
  • 6
  • 20
  • If your Ruby code produces a string with a line break, it will result in invalid JavaScript syntax. (And, apparently, that is happening.) – Pointy Feb 16 '21 at 02:27

1 Answers1

2

You could resolve it in 3 ways, using template literal or escape the single quote character

console.log(`<%= recaptcha_element('signup')%>`);

console.log("<%= recaptcha_element('signup')%>");

console.log('<%= recaptcha_element(\'signup\')%>');
hgb123
  • 13,869
  • 3
  • 20
  • 38