From 9415011b5fd192f1bafeaa9b6eacbb7921382a00 Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 19 May 2021 22:53:38 -0400 Subject: Chat --- app/controllers/chat_messages_controller.rb | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/controllers/chat_messages_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/chat_messages_controller.rb b/app/controllers/chat_messages_controller.rb new file mode 100644 index 0000000..9112bb7 --- /dev/null +++ b/app/controllers/chat_messages_controller.rb @@ -0,0 +1,32 @@ +class ChatMessagesController < ApplicationController + def index + # TODO: Let's rename this method to #list + @chat_messages = ChatMessage.order(created_at: :desc).limit(100).reverse + render partial: "chat_messages/list" + end + + def create + @chat_message = ChatMessage.new(body: chat_message_params[:body], + chat_room_id: chat_message_params[:chat_room_id], + sender: current_char) + # TODO: Make this block less bad + respond_to do |format| + if @chat_message.save + ActionCable.server.broadcast "chat_room_channel", + html: render_to_string(partial: "message", + locals: { + chat_message: @chat_message + }) + format.html { redirect_to character_path(current_char) } + format.js + else + format.html { redirect_to character_path(current_char), alert: "Failed to send chat message." } + end + end + end + + private + def chat_message_params + params.require(:chat_message).permit(:body, :chat_room_id) + end +end -- cgit v1.2.3