blob: 9112bb781b3bb4248c54f626cac8540f4ae01864 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
|