表題の通り、Rails5でActionCableでチャット作成していますが、リロードしないとメッセージが表示されません。
下記2ファイルのどこかが間違っているのではないかと推測はしていますが、そこからどのへんが怪しいか皆目検討つかないため、もし下記2ファイル中に怪しいところがあればご指摘いただけましたら幸いです。
ruby
1#room_channel.rb 2class RoomChannel < ApplicationCable::Channel 3 def subscribed 4 stream_from "room_channel" 5 end 6 7 def unsubscribed 8 end 9 10 def speak(data) 11 message = Message.create!(content: data['message']) 12 template = ApplicationController.renderer.render(parial: 'messages/message', locals: {message: message}) 13 ActionCable.server.broadcast 'room_channel', template 14 end 15end
ruby
1# room.js 2App.room = App.cable.subscriptions.create("RoomChannel", { 3 connected: function() { 4 console.log("connected") 5 // Called when the subscription is ready for use on the server 6 }, 7 8 disconnected: function() { 9 // Called when the subscription has been terminated by the server 10 }, 11 12 received: function(message) { 13 const messages = document.getElementById('messages') 14 messages.innerHTML += message 15 }, 16 17 speak: function(content) { 18 return this.perform('speak',{message: content}); 19 } 20}); 21 22document.addEventListener('DOMContentLoaded', function(){ 23 const input = document.getElementById('chat-input') 24 const button = document.getElementById('button') 25 button.addEventListener('click', function(){ 26 const content = input.value 27 App.room.speak(content) 28 input.value = '' 29 }) 30})
あなたの回答
tips
プレビュー