teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

修正

2020/04/06 08:06

投稿

yamazamaki
yamazamaki

スコア6

title CHANGED
File without changes
body CHANGED
@@ -155,27 +155,4 @@
155
155
  }
156
156
  });
157
157
  }
158
- ```
159
- ###chat_room_channel.rb
160
- ```
161
- class ChatRoomChannel < ApplicationCable::Channel
162
- def subscribed
163
- stream_for "chat_room_#{params[:room_id]}"
164
- end
165
-
166
- def unsubscribed
167
- # Any clkanup needed when channel is unsubscribed
168
- end
169
-
170
- def speak(data)
171
- message = ChatMessage.create(chat_room_id: data['room_id'], user_id: data['user_id'], message: data['message'])
172
- ChatRoomChannel.broadcast_to "chat_room_#{data['room_id']}", content: render_message(message)
173
- end
174
-
175
- private
176
- def render_message(message)
177
- ApplicationController.renderer.render(partial: 'chat_messages/chat_message', locals: { chat_message: message, current_user: message.user })
178
- end
179
-
180
- end
181
158
  ```

3

詳細なコードを追記しました。

2020/04/06 08:06

投稿

yamazamaki
yamazamaki

スコア6

title CHANGED
File without changes
body CHANGED
@@ -63,4 +63,119 @@
63
63
  ApplicationController.renderer.render(partial: 'chat_messages/chat_message', locals: { chat_message: message, current_user: message.user })
64
64
  end
65
65
  end
66
+ ```
67
+ ### chat/show.html.erb
68
+ ```
69
+ <header class="header">
70
+ <div class="chatPartner">
71
+ <%= link_to chat_index_path(), method: :get do%>
72
+ <span class="material-icons left">keyboard_arrow_left</span>
73
+ <% end %>
74
+ <div class="dm-username"><%= @chat_room_user.username %></div>
75
+ <div class="dm-userid">@<%= @chat_room_user.user_id %></div>
76
+ </div>
77
+ </header>
78
+ <div class="messagesArea messages" id="scroll-inner">
79
+ <div class="area">
80
+ <%= render @chat_messages %>
81
+ </div>
82
+ </div>
83
+ <div class="text-box">
84
+ <form class="messageInputForm">
85
+ <div class="messenger">
86
+ <textarea type="text" data-behavior="room_speaker" class="messageInputForm_input" placeholder="メッセージを入力..."></textarea>
87
+ </div>
88
+ </form>
89
+ </div>
90
+ </div>
91
+ </div>
92
+ ```
93
+ ###chat_controller.rb
94
+ ```
95
+ class ChatController < ApplicationController
96
+ def create
97
+ current_user_chat_rooms = ChatRoomUser.where(user_id: current_user.id).map(&:chat_room)
98
+ chat_room = ChatRoomUser.where(chat_room: current_user_chat_rooms, user_id: params[:user_id]).map(&:chat_room).first
99
+ if chat_room.blank?
100
+ chat_room = ChatRoom.create
101
+ ChatRoomUser.create(chat_room: chat_room, user_id: current_user.id)
102
+ ChatRoomUser.create(chat_room: chat_room, user_id: params[:user_id])
103
+ end
104
+ redirect_to action: :show, id: chat_room.id
105
+ end
106
+ def index
107
+ @current_user = current_user
108
+ chat_room = ChatRoom.find_by(id: params[:id])
109
+ @chat_rooms = ChatRoom.joins(:chat_room_users).
110
+ where("chat_room_users.user_id =?", @current_user.id).order(created_at: :desc)
111
+ @chat_messages = ChatMessage.where("chat_room_users.user_id =?", @current_user.id)
112
+ end
113
+
114
+
115
+ def show
116
+ chat_room = ChatRoom.find_by(id: params[:id])
117
+ @current_user = User.find_by(id: session[:user_id])
118
+ @chat_room_user = chat_room.chat_room_users.
119
+ where.not(user_id: current_user.id).first.user
120
+ @chat_room_myuser = chat_room.chat_room_users.
121
+ where.not(user_id: current_user.id).last.user
122
+ @chat_rooms = ChatRoom.joins(:chat_room_users).
123
+ where("chat_room_users.user_id =?", @current_user.id)
124
+ @chat_messages = ChatMessage.where(chat_room: chat_room).order(:created_at)
125
+ end
126
+ end
127
+ ```
128
+ ###chat.js
129
+ ```
130
+ if (/chat/.test(window.location.pathname)) {
131
+ var path = window.location.pathname.split('/');
132
+ var room_id = path[path.length - 1];
133
+ App.chat_room = App.cable.subscriptions.create({
134
+ channel: "ChatRoomChannel",
135
+ room_id: room_id
136
+ }, {
137
+ connected: function () {},
138
+ disconnected: function () {},
139
+ received: function (data) {
140
+ $('.messages').append(data['content']);
141
+ },
142
+ speak: function (message) {
143
+ return this.perform('speak', {
144
+ message: message,
145
+ room_id: room_id,
146
+ user_id: $('meta[name="current_user_id"]').attr('content')
147
+ });
148
+ }
149
+ });
150
+ $(document).on('keypress', '[data-behavior~=room_speaker]', function (event) {
151
+ if (event.keyCode === 13) {
152
+ App.chat_room.speak(event.target.value);
153
+ event.target.value = '';
154
+ return event.preventDefault();
155
+ }
156
+ });
157
+ }
158
+ ```
159
+ ###chat_room_channel.rb
160
+ ```
161
+ class ChatRoomChannel < ApplicationCable::Channel
162
+ def subscribed
163
+ stream_for "chat_room_#{params[:room_id]}"
164
+ end
165
+
166
+ def unsubscribed
167
+ # Any clkanup needed when channel is unsubscribed
168
+ end
169
+
170
+ def speak(data)
171
+ message = ChatMessage.create(chat_room_id: data['room_id'], user_id: data['user_id'], message: data['message'])
172
+ ChatRoomChannel.broadcast_to "chat_room_#{data['room_id']}", content: render_message(message)
173
+ end
174
+
175
+ private
176
+ def render_message(message)
177
+ ApplicationController.renderer.render(partial: 'chat_messages/chat_message', locals: { chat_message: message, current_user: message.user })
178
+ end
179
+
180
+ end
66
181
  ```

2

画像の追加

2020/04/05 16:08

投稿

yamazamaki
yamazamaki

スコア6

title CHANGED
File without changes
body CHANGED
@@ -6,9 +6,10 @@
6
6
  足りない記述があれば付け足しさせていただきます。
7
7
  詳しい方教えていただけると幸いです。
8
8
 
9
-
9
+ 画像1
10
10
  ![イメージ説明](66e5383ce73d89a7b39ea1e7effd9b7d.png)
11
-
11
+ 画像2
12
+ ![画像2](7f8f8c3e7a62a6f3fd2761a775327ed4.png)
12
13
  ### _chat_message.html.erb
13
14
  ```
14
15
  <% if chat_message.user != current_user %>

1

タイトルの変更

2020/04/05 13:44

投稿

yamazamaki
yamazamaki

スコア6

title CHANGED
@@ -1,1 +1,1 @@
1
- チャット画面でif文が効かない
1
+ チャット画面で文章を送信したときにif文が効かない
body CHANGED
File without changes