跳至内容 跳至搜索
方法
A

实例公共方法

action_cable_meta_tag()

返回一个“action-cable-url”元标签,其值为配置中指定的 URL。 确保此标签位于您的 JavaScript 标签之上

<head>
  <%= action_cable_meta_tag %>
  <%= javascript_include_tag 'application', 'data-turbo-track' => 'reload' %>
</head>

然后,Action Cable 使用它来确定 WebSocket 服务器的 URL。 您的 JavaScript 随后可以连接到服务器,而无需直接指定 URL

import Cable from "@rails/actioncable"
window.Cable = Cable
window.App = {}
App.cable = Cable.createConsumer()

确保在每个环境配置文件中指定正确的服务器位置

config.action_cable.mount_path = "/cable123"
<%= action_cable_meta_tag %> would render:
=> <meta name="action-cable-url" content="/cable123" />

config.action_cable.url = "ws://actioncable.com"
<%= action_cable_meta_tag %> would render:
=> <meta name="action-cable-url" content="ws://actioncable.com" />
# File actioncable/lib/action_cable/helpers/action_cable_helper.rb, line 36
def action_cable_meta_tag
  tag "meta", name: "action-cable-url", content: (
    ActionCable.server.config.url ||
    ActionCable.server.config.mount_path ||
    raise("No Action Cable URL configured -- please configure this at config.action_cable.url")
  )
end