2017-04-30 10 views
0

私は現在公式Rails ActionCable guideに従っています。Rails Actioncable - 定期購読のための正しい場所?

それはapp/assets/javascripts/cable/subscriptions/channelname.coffeeでサブスクリプション(App.cable.subscriptions.create{...})用のファイルを作成示唆 - 私はコマンドrails g channel channelname methodnameを使用する場合、それは私もこのguide at nopioを追ってきたapp/assets/javascripts/channels/channelname.coffee

で対応するファイルを作成し、それがあるべきと言うことJavaScriptファイル - app/assets/javascripts/channels/channelname.js

私はRuby 2.3.3とRails 5.0.2を使用しています。どの場所とファイルタイプが正しいのですか? ご協力いただきありがとうございます! :)

答えて

0

これはコードの2つの文字列です。なぜその特別なファイルに使用するのですか?私は反応して、通知コンポーネントに置きます:

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) 
    } 
} 

アプリケーションに応じて一般的なコードとして配置してください。

関連する問題