我遇到的问题:



这是我的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或添加therubyracer gem

sudo apt-get install nodejs


要么
therubyracer中添加Gemfile宝石

gem 'therubyracer'

10-05 22:28