Uncaught SyntaxError: '' string literal contains an unescaped line break

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?

2 thoughts on “Uncaught SyntaxError: '' string literal contains an unescaped line break”

  1. 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\')%>');
    Reply

Leave a Comment