【问题标题】:Rails Actioncable - correct place for subscription?Rails Actioncable - 订阅的正确位置?
【发布时间】:2017-09-28 07:05:38
【问题描述】:

我目前正在关注官方Rails ActionCable guide

它建议在app/assets/javascripts/cable/subscriptions/channelname.coffee 中为订阅(App.cable.subscriptions.create{...})创建一个文件——但是当我使用命令rails g channel channelname methodname 时,它会在app/assets/javascripts/channels/channelname.coffee 中创建相应的文件

我也一直在关注这个guide at nopio,说它应该是一个 JavaScript 文件 - app/assets/javascripts/channels/channelname.js

我正在使用 Ruby 2.3.3 和 Rails 5.0.2。哪个地方和哪个文件类型是正确的? 谢谢你的帮助! :)

【问题讨论】:

    标签: ruby-on-rails-5 actioncable


    【解决方案1】:

    只有一两串代码。为什么要用于那个特殊文件。我使用 react 并将其放在我的通知组件中:

    class Notifications extends React.Component {
      constructor(props) {
        super(props);
        this.subscribeNofications()
      }
    
      subscribeNofications = () => {
        this._notificationChannel = App.cable.subscriptions.create(
          { channel: "NotificationsChannel", room: this.getUserChannel() }, {
          received: this.showNotification
        })
      }
    
      showNofication = (data) => {
        alert(data.message)
      }
    }
    

    所以根据你的应用把它作为通用代码。

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2019-03-19
      • 2018-06-14
      • 1970-01-01
      • 2017-03-28
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      相关资源
      最近更新 更多