未捕获的 SyntaxError: '' 字符串文字包含未转义的换行符

问题描述

我使用的是 Ruby on Rails。

create.js.erb

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

但我收到了 Uncaught SyntaxError: '' string literal contains an unescaped line break 错误

我的视图助手看起来像这样。我正在使用recaptcha gem。

帮手

  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

我的语法有什么问题?

解决方法

您可以通过 3 种方式解决它,使用模板文字或转义单引号字符

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

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

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