ExecJS::RuntimeError 在聊天室#show

ExecJS::RuntimeError in ChatRooms#show

我遇到的问题:

这是我的 room.coffee 文件,一切正常。

jQuery(document).on 'turbolinks:load', ->
  messages = $('#messages')
  if $('#messages').length > 0

    App.global_chat = App.cable.subscriptions.create {
        channel: "ChatRoomsChannel"
        chat_room_id: messages.data('chat-room-id')
      },
      connected: ->
        # Called when the subscription is ready for use on the server

      disconnected: ->
        # Called when the subscription has been terminated by the server

      received: (data) ->
        # Data received

      send_message: (message, chat_room_id) ->
        @perform 'send_message', message: message, chat_room_id: chat_room_id
@import "bootstrap-sprockets";
@import "bootstrap";

#messages {
  max-height: 450px;
  overflow-y: auto;
  .avatar {
    margin: 0.5rem;
  }
}
<h1><%= @chat_room.title %></h1>

<div id="messages" data-chat-room-id="<%= @chat_room.id %>">
  <%= render @chat_room.messages %>
</div>

<hr>

<%= form_for @message, url: '#' do |f| %>
  <%= hidden_field_tag 'chat_room_id', @chat_room.id %>
  <div class="form-group">
    <%= f.label :body %>
    <%= f.text_area :body, class: 'form-control' %>
    <small class="text-muted">From 2 to 1000 characters</small>
  </div>

  <%= f.submit "Post", class: 'btn btn-primary btn-lg' %>
<% end %>

但是在我在同一个文件中添加下面的代码后它显示 ExecJS::RuntimeError。

$('#new_message').submit (e) ->
      $this = $(this)
      textarea = $this.find('#message_body')
      if $.trim(textarea.val()).length > 1
        App.global_chat.send_message textarea.val(), messages.data('chat-room-id')
        textarea.val('')
      e.preventDefault()
      return false

类似的问题我也搜了很多,用pc的大部分都遇到过这个问题。但是我正在使用 mac,我不知道为什么我也会收到这个错误。

您需要javascript运行时环境

安装node.js或添加therubyracergem

sudo apt-get install nodejs

在你的 Gemfile

中添加 therubyracer gem
gem 'therubyracer'

在您的 room.coffee 文件中,您的代码使用两个 space 缩进。在您添加的代码中,缩进不止于此。如果您将 $('#new_message').submit (e) -> 之后的部分缩进以匹配两个 space 缩进,语法错误将得到解决。