質問編集履歴

4

修正

2020/04/06 08:06

投稿

yamazamaki
yamazamaki

スコア6

test CHANGED
File without changes
test CHANGED
@@ -313,49 +313,3 @@
313
313
  }
314
314
 
315
315
  ```
316
-
317
- ###chat_room_channel.rb
318
-
319
- ```
320
-
321
- class ChatRoomChannel < ApplicationCable::Channel
322
-
323
- def subscribed
324
-
325
- stream_for "chat_room_#{params[:room_id]}"
326
-
327
- end
328
-
329
-
330
-
331
- def unsubscribed
332
-
333
- # Any clkanup needed when channel is unsubscribed
334
-
335
- end
336
-
337
-
338
-
339
- def speak(data)
340
-
341
- message = ChatMessage.create(chat_room_id: data['room_id'], user_id: data['user_id'], message: data['message'])
342
-
343
- ChatRoomChannel.broadcast_to "chat_room_#{data['room_id']}", content: render_message(message)
344
-
345
- end
346
-
347
-
348
-
349
- private
350
-
351
- def render_message(message)
352
-
353
- ApplicationController.renderer.render(partial: 'chat_messages/chat_message', locals: { chat_message: message, current_user: message.user })
354
-
355
- end
356
-
357
-
358
-
359
- end
360
-
361
- ```

3

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

2020/04/06 08:06

投稿

yamazamaki
yamazamaki

スコア6

test CHANGED
File without changes
test CHANGED
@@ -129,3 +129,233 @@
129
129
  end
130
130
 
131
131
  ```
132
+
133
+ ### chat/show.html.erb
134
+
135
+ ```
136
+
137
+ <header class="header">
138
+
139
+ <div class="chatPartner">
140
+
141
+ <%= link_to chat_index_path(), method: :get do%>
142
+
143
+ <span class="material-icons left">keyboard_arrow_left</span>
144
+
145
+ <% end %>
146
+
147
+ <div class="dm-username"><%= @chat_room_user.username %></div>
148
+
149
+ <div class="dm-userid">@<%= @chat_room_user.user_id %></div>
150
+
151
+ </div>
152
+
153
+ </header>
154
+
155
+ <div class="messagesArea messages" id="scroll-inner">
156
+
157
+ <div class="area">
158
+
159
+ <%= render @chat_messages %>
160
+
161
+ </div>
162
+
163
+ </div>
164
+
165
+ <div class="text-box">
166
+
167
+ <form class="messageInputForm">
168
+
169
+ <div class="messenger">
170
+
171
+ <textarea type="text" data-behavior="room_speaker" class="messageInputForm_input" placeholder="メッセージを入力..."></textarea>
172
+
173
+ </div>
174
+
175
+ </form>
176
+
177
+ </div>
178
+
179
+ </div>
180
+
181
+ </div>
182
+
183
+ ```
184
+
185
+ ###chat_controller.rb
186
+
187
+ ```
188
+
189
+ class ChatController < ApplicationController
190
+
191
+ def create
192
+
193
+ current_user_chat_rooms = ChatRoomUser.where(user_id: current_user.id).map(&:chat_room)
194
+
195
+ chat_room = ChatRoomUser.where(chat_room: current_user_chat_rooms, user_id: params[:user_id]).map(&:chat_room).first
196
+
197
+ if chat_room.blank?
198
+
199
+ chat_room = ChatRoom.create
200
+
201
+ ChatRoomUser.create(chat_room: chat_room, user_id: current_user.id)
202
+
203
+ ChatRoomUser.create(chat_room: chat_room, user_id: params[:user_id])
204
+
205
+ end
206
+
207
+ redirect_to action: :show, id: chat_room.id
208
+
209
+ end
210
+
211
+ def index
212
+
213
+ @current_user = current_user
214
+
215
+ chat_room = ChatRoom.find_by(id: params[:id])
216
+
217
+ @chat_rooms = ChatRoom.joins(:chat_room_users).
218
+
219
+ where("chat_room_users.user_id =?", @current_user.id).order(created_at: :desc)
220
+
221
+ @chat_messages = ChatMessage.where("chat_room_users.user_id =?", @current_user.id)
222
+
223
+ end
224
+
225
+
226
+
227
+
228
+
229
+ def show
230
+
231
+ chat_room = ChatRoom.find_by(id: params[:id])
232
+
233
+ @current_user = User.find_by(id: session[:user_id])
234
+
235
+ @chat_room_user = chat_room.chat_room_users.
236
+
237
+ where.not(user_id: current_user.id).first.user
238
+
239
+ @chat_room_myuser = chat_room.chat_room_users.
240
+
241
+ where.not(user_id: current_user.id).last.user
242
+
243
+ @chat_rooms = ChatRoom.joins(:chat_room_users).
244
+
245
+ where("chat_room_users.user_id =?", @current_user.id)
246
+
247
+ @chat_messages = ChatMessage.where(chat_room: chat_room).order(:created_at)
248
+
249
+ end
250
+
251
+ end
252
+
253
+ ```
254
+
255
+ ###chat.js
256
+
257
+ ```
258
+
259
+ if (/chat/.test(window.location.pathname)) {
260
+
261
+ var path = window.location.pathname.split('/');
262
+
263
+ var room_id = path[path.length - 1];
264
+
265
+ App.chat_room = App.cable.subscriptions.create({
266
+
267
+ channel: "ChatRoomChannel",
268
+
269
+ room_id: room_id
270
+
271
+ }, {
272
+
273
+ connected: function () {},
274
+
275
+ disconnected: function () {},
276
+
277
+ received: function (data) {
278
+
279
+ $('.messages').append(data['content']);
280
+
281
+ },
282
+
283
+ speak: function (message) {
284
+
285
+ return this.perform('speak', {
286
+
287
+ message: message,
288
+
289
+ room_id: room_id,
290
+
291
+ user_id: $('meta[name="current_user_id"]').attr('content')
292
+
293
+ });
294
+
295
+ }
296
+
297
+ });
298
+
299
+ $(document).on('keypress', '[data-behavior~=room_speaker]', function (event) {
300
+
301
+ if (event.keyCode === 13) {
302
+
303
+ App.chat_room.speak(event.target.value);
304
+
305
+ event.target.value = '';
306
+
307
+ return event.preventDefault();
308
+
309
+ }
310
+
311
+ });
312
+
313
+ }
314
+
315
+ ```
316
+
317
+ ###chat_room_channel.rb
318
+
319
+ ```
320
+
321
+ class ChatRoomChannel < ApplicationCable::Channel
322
+
323
+ def subscribed
324
+
325
+ stream_for "chat_room_#{params[:room_id]}"
326
+
327
+ end
328
+
329
+
330
+
331
+ def unsubscribed
332
+
333
+ # Any clkanup needed when channel is unsubscribed
334
+
335
+ end
336
+
337
+
338
+
339
+ def speak(data)
340
+
341
+ message = ChatMessage.create(chat_room_id: data['room_id'], user_id: data['user_id'], message: data['message'])
342
+
343
+ ChatRoomChannel.broadcast_to "chat_room_#{data['room_id']}", content: render_message(message)
344
+
345
+ end
346
+
347
+
348
+
349
+ private
350
+
351
+ def render_message(message)
352
+
353
+ ApplicationController.renderer.render(partial: 'chat_messages/chat_message', locals: { chat_message: message, current_user: message.user })
354
+
355
+ end
356
+
357
+
358
+
359
+ end
360
+
361
+ ```

2

画像の追加

2020/04/05 16:08

投稿

yamazamaki
yamazamaki

スコア6

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,13 @@
14
14
 
15
15
 
16
16
 
17
-
17
+ 画像1
18
18
 
19
19
  ![イメージ説明](66e5383ce73d89a7b39ea1e7effd9b7d.png)
20
20
 
21
+ 画像2
21
22
 
23
+ ![画像2](7f8f8c3e7a62a6f3fd2761a775327ed4.png)
22
24
 
23
25
  ### _chat_message.html.erb
24
26
 

1

タイトルの変更

2020/04/05 13:44

投稿

yamazamaki
yamazamaki

スコア6

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