前提・実現したいこと
railsでLINEのようなチャットアプリを作成しています。
Action Cableを用いることで非同期でチャット相手に対してメッセージを表示させることはできました、しかしAction Cableを用いているにもかかわらず送信者には送信したメッセージが非同期で表示されません。これを解決したいです。
参考にさせていただいた記事です
https://hajimeteblog.com/rails-actioncable/
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
送信したメッセージはこちらのmessage_controller.rbにいきます
ruby
1def create 2 @room = Room.find(params[:rom_id] 3 @message = Message.new(user_comment_params) 4 respond_to do |format| 5 if @message.save 6 ActionCable.server.broadcast 'message_channel', content: @message 7 format.html { redirect_to room_path(@room) } 8 format.json { render template: "rooms/show", status: :created, location: @room } 9 format.js 10 else 11 @messages = Message.where(room_id: @room).order("created_at ASC") 12 @celebs = Celeb.all 13 @results = @p.result 14 @prices = Price.all 15 format.html { render template: "rooms/show"} 16 format.json { render json: @message.errors, status: :unprocessable_entity } 17 end 18 end 19 end
表示されているhtmlはこちらになります
show.html.erb
ruby
1 <% @messages.each do |message|%> 2 <% if user_signed_in? %> 3 <% if message.user_id == current_user.id %> 4 <div class="mycomment" > 5 <%= link_to '削除する', room_message_path(@room,message), method: :delete, class: "delete-btn hidden", id: "destroy"%> 6 <%= message.read ? '既読' : ' '%> 7 <%= l message.created_at, format: :short%> 8 <p class='my'><%= message.content %></p> 9 </div> 10 <% else %> 11 <div class="fukidasi"> 12 <div class="faceicon"> 13 <%= image_tag @room.celeb.image, class: 'img' %> 14 </div> 15 <div class="chatting"> 16 <div class="says"> 17 <p><%= message.content %></p> 18 </div> 19 <div class='created_at'> 20 <%= l message.created_at, format: :short %> 21 </div> 22 </div> 23 </div> 24 <% end %> 25 <% else %> 26 <% end %>
Action Cableによってbroadcastされるchannelです
message_channel.rb
ruby
1class MessageChannel < ApplicationCable::Channel 2 def subscribed 3 stream_from "message_channel" 4 end 5 6 def unsubscribed 7 # Any cleanup needed when channel is unsubscribed 8 end 9end
こちらがmessage_channel.rbです
ruby
1import consumer from "./consumer" 2 3consumer.subscriptions.create("MessageChannel", { 4 connected() { 5 // Called when the subscription is ready for use on the server 6 }, 7 8 disconnected() { 9 // Called when the subscription has been terminated by the server 10 }, 11 12 received(data) { 13 const today = new Date(); 14 const month = ("0"+(today.getMonth() + 1)).slice(-2); 15 const day = ("0"+today.getDay()).slice(-2); 16 const hours = today.getHours(); 17 const minutes = ("0"+today.getMinutes()).slice(-2); 18 const html = `<div class="mycomment" ><a href= "/rooms/${data.content.room_id}/messages/${data.content.id}" class="delete-btn hidden" rel="nofollow">削除する</a>${month}/${day} ${hours}:${minutes} <p>${data.content.content}</p></div>`; 19 const messages = document.getElementById('messages'); 20 const newMessage = document.getElementById('message_content') 21 const messageButton = document.getElementById('message-button') 22 messages.insertAdjacentHTML('beforeend', html); 23 newMessage.value=''; 24 messageButton.removeAttribute('disabled'); 25 26 const obj = document.getElementById("messages"); 27 obj.scrollTop = obj.scrollHeight; 28 29 } 30});
試したこと
message_controller.rbのcreareアクションでformatがhtmlの時にredirect_toとなっているのでページがリロードされるので自分が解決したいことの原因はこれでは無いのかなと思っています。
しかし、これを無くした場合htmlのformatを表示できないとエラーが出ました。
何か良い方法があればなんでも教えてください。即レスします!!
補足情報(FW/ツールのバージョンなど)
rails [6.0]
あなたの回答
tips
プレビュー