diff options
author | David Gay <david@davidgay.org> | 2021-05-19 22:53:38 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-19 22:53:38 -0400 |
commit | 9415011b5fd192f1bafeaa9b6eacbb7921382a00 (patch) | |
tree | f25d9d633237cae5d7b73166e6612a9b53312714 /app/controllers | |
parent | da678b22b5db05554b44234b341fabc9d83ff700 (diff) |
Chat
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/chat_messages_controller.rb | 32 |
1 files changed, 32 insertions, 0 deletions
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 |