Ruby on Rails -- CoffeeScript 中的 ActionView::Template::Error(SyntaxError unexpected if)

Ruby on Rails -- ActionView::Template::Error (SyntaxError unexpected if) in CoffeeScript

我有以下 .js.coffee 文件:

$('#search-results').html('<%= j render @articles %>');

$(document).ready () -> 
  dialog = document.querySelector('dialog');

  if (!dialog.showModal) {
    dialogPolyfill.registerDialog(dialog);
  }

  dialog.showModal();

  dialog.querySelector '.close', addEventListener 'click', () ->
    dialog.close();

为什么我会收到错误消息?

在 CoffeeScript 中,您使用缩进而不是大括号。

替换

if (!dialog.showModal) {
  dialogPolyfill.registerDialog(dialog);
}

if (!dialog.showModal)
  dialogPolyfill.registerDialog(dialog)

我建议通过 documentation