質問編集履歴

7

タイトル・内容の変更

2021/02/23 15:28

投稿

Aki-F
Aki-F

スコア0

test CHANGED
File without changes
test CHANGED
@@ -20,340 +20,212 @@
20
20
 
21
21
  ### 該当するソースコード
22
22
 
23
+ ```javascript
24
+
25
+ -- message_channel.js --
26
+
27
+ import consumer from "./consumer"
28
+
29
+
30
+
31
+ consumer.subscriptions.create("MessageChannel", {
32
+
33
+ connected() {
34
+
35
+ // Called when the subscription is ready for use on the server
36
+
37
+ },
38
+
39
+
40
+
41
+ disconnected() {
42
+
43
+ // Called when the subscription has been terminated by the server
44
+
45
+ },
46
+
47
+
48
+
49
+ received(data) {
50
+
51
+ const nickName = `${data.user.nickname}`
52
+
53
+ const content = `${data.content.content}`
54
+
55
+ const messageData = `${data.time}`
56
+
57
+ const currentUser = `${data.user.id}`
58
+
59
+ const sendUser = `${data.content.user_id}`
60
+
61
+ if (currentUser == sendUser) {
62
+
63
+ const HTML = `
64
+
65
+ <div class="my-content">
66
+
67
+ <div class="my-message">
68
+
69
+ <div class="my-message-date">
70
+
71
+ ${messageData}
72
+
73
+ </div>
74
+
75
+ <div class="my-message-content">
76
+
77
+ ${content}
78
+
79
+ </div>
80
+
81
+ </div>
82
+
83
+ </div>
84
+
85
+ `
86
+
87
+ const messages = document.getElementById('message')
88
+
89
+ const newMessage = document.getElementById('content')
90
+
91
+ messages.insertAdjacentHTML('beforeend', HTML)
92
+
93
+ newMessage.value=''
94
+
95
+ } else {
96
+
97
+ const HTML = `
98
+
99
+ <div class="content">
100
+
101
+ <div class="upper-message">
102
+
103
+ <div class="message-user">
104
+
105
+ ${nickName}
106
+
107
+ </div>
108
+
109
+ </div>
110
+
111
+ <div class="lower-message">
112
+
113
+ <div class="message-content" >
114
+
115
+ ${content}
116
+
117
+ </div>
118
+
119
+ <div class="message-date">
120
+
121
+ ${messageData}
122
+
123
+ </div>
124
+
125
+ </div>
126
+
127
+ </div>
128
+
129
+ `
130
+
131
+ const messages = document.getElementById('message')
132
+
133
+ const newMessage = document.getElementById('content')
134
+
135
+ messages.insertAdjacentHTML('beforeend', HTML)
136
+
137
+ newMessage.value=''
138
+
139
+ }
140
+
141
+ }
142
+
143
+ });
144
+
145
+ ```
146
+
147
+
148
+
23
- ```Ruby
149
+ ```ruby
24
-
150
+
25
- -- Message_controller.rb --
151
+ -- message_channel.rb --
26
-
152
+
27
- class MessagesController < ApplicationController
153
+ class MessageChannel < ApplicationCable::Channel
28
-
29
- before_action :set_message, only: %i[index create]
154
+
30
-
31
-
32
-
33
- def index
155
+ def subscribed
34
-
35
- @user = @room.user_id
156
+
36
-
37
- @messages = @room.messages.includes(:user).order(:id)
38
-
39
- @message = current_user.messages.build
157
+ stream_from 'message_channel'
40
158
 
41
159
  end
42
160
 
43
161
 
44
162
 
45
- def create
163
+ def unsubscribed
46
-
164
+
47
- @message = Message.create!(message_params)
165
+ # Any cleanup needed when channel is unsubscribed
48
-
49
- ActionCable.server.broadcast 'message_channel', content: @message, user: current_user,
50
-
51
- time: @message.created_at.strftime('%Y/%m/%d %H:%M:%S')
52
166
 
53
167
  end
54
168
 
55
-
169
+ end
170
+
56
-
171
+ ```
172
+
173
+
174
+
175
+ ```ruby
176
+
177
+ -- application_cable/connection.rb --
178
+
179
+ module ApplicationCable
180
+
181
+ class Connection < ActionCable::Connection::Base
182
+
183
+ identified_by :current_user
184
+
185
+
186
+
187
+ def connect
188
+
189
+ self.current_user = find_verified_user
190
+
191
+ end
192
+
193
+
194
+
57
- private
195
+ private
58
-
59
-
60
-
196
+
197
+
198
+
61
- def message_params
199
+ def find_verified_user
62
-
200
+
63
- params.require(:message).permit(:content, :image).merge(user_id: current_user.id, room_id: params[:room_id])
201
+ if verified_user = User.find_by(id: cookies.signed[:user_id])
202
+
203
+ verified_user
204
+
205
+ else
206
+
207
+ reject_unauthorized_connection
208
+
209
+ end
210
+
211
+ end
64
212
 
65
213
  end
66
214
 
67
-
68
-
69
- def set_message
70
-
71
- @room = Room.find(params[:room_id])
72
-
73
- end
74
-
75
215
  end
76
216
 
77
217
  ```
78
218
 
79
219
 
80
220
 
81
- ```ruby
82
-
83
- -- _message.html.erb --
84
-
85
- <div class="message" id="message">
86
-
87
- <% @messages.each do |message| %>
88
-
89
- <% if current_user.id == message.user_id %>
90
-
91
- <div class="my-content" id="my-message">
92
-
93
- <div class="my-message">
94
-
95
- <div class="my-message-date">
96
-
97
- <%= l message.created_at %>
98
-
99
- </div>
100
-
101
- <div class="my-message-content" >
102
-
103
- <%= simple_format(h message.content) %>
104
-
105
- </div>
106
-
107
- <%# <%= image_tag message.image.variant(resize: '500x500'), class: 'my-message-image' if message.image.attached? %>
108
-
109
- </div>
110
-
111
- </div>
112
-
113
- <% else %>
114
-
115
- <div class="content">
116
-
117
- <div class="upper-message">
118
-
119
- <div class="message-user">
120
-
121
- <%= message.user.nickname %>
122
-
123
- </div>
124
-
125
- </div>
126
-
127
- <div class="lower-message">
128
-
129
- <div class="message-content" >
130
-
131
- <%= simple_format(h message.content) %>
132
-
133
- </div>
134
-
135
- <div class="message-date">
136
-
137
- <%= l message.created_at %>
138
-
139
- </div>
140
-
141
- <%# <%= image_tag message.image.variant(resize: '500x500'), class: 'message-image' if message.image.attached? %>
142
-
143
- </div>
144
-
145
- </div>
146
-
147
- <% end %>
148
-
149
- <% end %>
150
-
151
- </div>
152
-
153
- ```
154
-
155
-
156
-
157
-
158
-
159
- ```javascript
160
-
161
- -- message_channel.js --
162
-
163
- import consumer from "./consumer"
164
-
165
-
166
-
167
- consumer.subscriptions.create("MessageChannel", {
168
-
169
- connected() {
170
-
171
- // Called when the subscription is ready for use on the server
172
-
173
- },
174
-
175
-
176
-
177
- disconnected() {
178
-
179
- // Called when the subscription has been terminated by the server
180
-
181
- },
182
-
183
-
184
-
185
- received(data) {
186
-
187
- const nickName = `${data.user.nickname}`
188
-
189
- const content = `${data.content.content}`
190
-
191
- const messageData = `${data.time}`
192
-
193
- const currentUser = `${data.user.id}`
194
-
195
- const sendUser = `${data.content.user_id}`
196
-
197
- if (currentUser == sendUser) {
198
-
199
- const HTML = `
200
-
201
- <div class="my-content">
202
-
203
- <div class="my-message">
204
-
205
- <div class="my-message-date">
206
-
207
- ${messageData}
208
-
209
- </div>
210
-
211
- <div class="my-message-content">
212
-
213
- ${content}
214
-
215
- </div>
216
-
217
- </div>
218
-
219
- </div>
220
-
221
- `
222
-
223
- const messages = document.getElementById('message')
224
-
225
- const newMessage = document.getElementById('content')
226
-
227
- messages.insertAdjacentHTML('beforeend', HTML)
228
-
229
- newMessage.value=''
230
-
231
- } else {
232
-
233
- const HTML = `
234
-
235
- <div class="content">
236
-
237
- <div class="upper-message">
238
-
239
- <div class="message-user">
240
-
241
- ${nickName}
242
-
243
- </div>
244
-
245
- </div>
246
-
247
- <div class="lower-message">
248
-
249
- <div class="message-content" >
250
-
251
- ${content}
252
-
253
- </div>
254
-
255
- <div class="message-date">
256
-
257
- ${messageData}
258
-
259
- </div>
260
-
261
- </div>
262
-
263
- </div>
264
-
265
- `
266
-
267
- const messages = document.getElementById('message')
268
-
269
- const newMessage = document.getElementById('content')
270
-
271
- messages.insertAdjacentHTML('beforeend', HTML)
272
-
273
- newMessage.value=''
274
-
275
- }
276
-
277
- }
278
-
279
- });
280
-
281
- ```
282
-
283
-
284
-
285
- ```ruby
286
-
287
- -- message_channel.rb --
288
-
289
- class MessageChannel < ApplicationCable::Channel
290
-
291
- def subscribed
292
-
293
- stream_from 'message_channel'
294
-
295
- end
296
-
297
-
298
-
299
- def unsubscribed
300
-
301
- # Any cleanup needed when channel is unsubscribed
302
-
303
- end
304
-
305
- end
306
-
307
- ```
308
-
309
-
310
-
311
- ```ruby
312
-
313
- -- application_cable/connection.rb --
314
-
315
- module ApplicationCable
316
-
317
- class Connection < ActionCable::Connection::Base
318
-
319
- identified_by :current_user
320
-
321
-
322
-
323
- def connect
324
-
325
- self.current_user = find_verified_user
326
-
327
- end
328
-
329
-
330
-
331
- private
332
-
333
-
334
-
335
- def find_verified_user
336
-
337
- if verified_user = User.find_by(id: cookies.signed[:user_id])
338
-
339
- verified_user
340
-
341
- else
342
-
343
- reject_unauthorized_connection
344
-
345
- end
346
-
347
- end
348
-
349
- end
350
-
351
- end
352
-
353
- ```
354
-
355
-
356
-
357
- Messages_controllerにて、ActionCableに渡すインスタンスの変更、connection.rbでcurrent_userを定義したりを行いましたが、受信者と送信者でビューを変えることができませんでした。
358
-
359
- 恐れ入りますが、どなたかご教授いただきたいです。
221
+ ### 行ったこと
222
+
223
+ connection.rbでcurrent_userを定義しました。
224
+
225
+
226
+
227
+ ### 解決したいこと
228
+
229
+ 受信者と送信者のチャット画面でメッセージの表示位置を変えることができませんでした。
230
+
231
+ 初学者のため拙いコードで申し訳ございませんが、どなたかご教授いただきたいです。

6

初心者マークを付与

2021/02/23 15:28

投稿

Aki-F
Aki-F

スコア0

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- 解決方法をご教授いただきたいです。
11
+ どなたか解決方法をご教授いただきたいです。
12
12
 
13
13
 
14
14
 

5

内容説明の補足

2021/02/23 10:45

投稿

Aki-F
Aki-F

スコア0

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  リアルタイムチャット自体は実装できたのですが、別ブラウザで機能確認を行ったところ、ログイン中のユーザーidがうまく取得できず、送信側の表示になってしまいます。リロードを行うと送信(右側)受信(左側)それぞれ正常な位置に戻ります。
6
6
 
7
+ 送信者・受信者は、message_channel.jsファイルのreceived(date)で条件分岐を行っています。
8
+
7
9
 
8
10
 
9
11
  解決方法をご教授いただきたいです。

4

タイトル・内容の変更

2021/02/23 08:27

投稿

Aki-F
Aki-F

スコア0

test CHANGED
@@ -1 +1 @@
1
- ActionCableでログイン中のユーザーの取得方法が知りたいです
1
+ ActionCableでcurrent_userIDを取得する方法をご教授いだきたいです
test CHANGED
@@ -1,32 +1,36 @@
1
1
  ### 解決したいこと
2
2
 
3
- Ruby on RailsのActionCableを使用して対話型のラインのようなチャット機能を作成しています。
3
+ Ruby on Rails対話型のラインのようなチャット機能をActionCableを使用して作成しています。
4
-
4
+
5
- リアルタイムチャット自体は実装できたのですが、別ブラウザ+別アカウントで機能確認を行ったところ、ログイン中のユーザーidうまく取得できていない様子で片方で送信したメッセージがどちらの画面でも送信側の表示になってしまいます。リロードを行うと送信受信それぞれ正常な位置に戻ります。
5
+ リアルタイムチャット自体は実装できたのですが、別ブラウザで機能確認を行ったところ、ログイン中のユーザーidうまく取得でき、送信側の表示になってしまいます。リロードを行うと送信(右側)受信(左側)それぞれ正常な位置に戻ります。
6
+
7
+
8
+
9
+ 解決方法をご教授いただきたいです。
10
+
11
+
12
+
13
+ ### 発生している問題・エラー
6
14
 
7
15
  下記Gifのような現象が発生しています。
8
16
 
9
- 解決方法をご教授いただきたいです。
10
-
11
-
12
-
13
- ### 発生している問題・エラー
14
-
15
- [リアルタイムチャット画面](https://i.gyazo.com/45f5a2439c852f3abb962ce6ab66ba58.gif)
17
+ [リアルタイムチャット](https://user-images.githubusercontent.com/75027499/108735166-8b148a00-7573-11eb-9699-76536a22ad0b.gif)
16
-
17
-
18
18
 
19
19
  ### 該当するソースコード
20
20
 
21
21
  ```Ruby
22
22
 
23
+ -- Message_controller.rb --
24
+
23
25
  class MessagesController < ApplicationController
24
26
 
25
- before_action :set_message, only: [:index, :create]
27
+ before_action :set_message, only: %i[index create]
28
+
29
+
26
30
 
27
31
  def index
28
32
 
29
- @user = RoomUser.where(user_id: @room).first
33
+ @user = @room.user_id
30
34
 
31
35
  @messages = @room.messages.includes(:user).order(:id)
32
36
 
@@ -34,13 +38,15 @@
34
38
 
35
39
  end
36
40
 
41
+
42
+
37
43
  def create
38
44
 
39
- @user = current_user
40
-
41
45
  @message = Message.create!(message_params)
42
46
 
43
- ActionCable.server.broadcast "message_channel", content: @message, user: @user, time: @message.created_at.strftime("%Y/%m/%d %H:%M:%S")
47
+ ActionCable.server.broadcast 'message_channel', content: @message, user: current_user,
48
+
49
+ time: @message.created_at.strftime('%Y/%m/%d %H:%M:%S')
44
50
 
45
51
  end
46
52
 
@@ -48,6 +54,8 @@
48
54
 
49
55
  private
50
56
 
57
+
58
+
51
59
  def message_params
52
60
 
53
61
  params.require(:message).permit(:content, :image).merge(user_id: current_user.id, room_id: params[:room_id])
@@ -70,6 +78,8 @@
70
78
 
71
79
  ```ruby
72
80
 
81
+ -- _message.html.erb --
82
+
73
83
  <div class="message" id="message">
74
84
 
75
85
  <% @messages.each do |message| %>
@@ -92,7 +102,7 @@
92
102
 
93
103
  </div>
94
104
 
95
- <%= image_tag message.image.variant(resize: '500x500'), class: 'my-message-image' if message.image.attached? %>
105
+ <%# <%= image_tag message.image.variant(resize: '500x500'), class: 'my-message-image' if message.image.attached? %>
96
106
 
97
107
  </div>
98
108
 
@@ -126,7 +136,7 @@
126
136
 
127
137
  </div>
128
138
 
129
- <%= image_tag message.image.variant(resize: '500x500'), class: 'message-image' if message.image.attached? %>
139
+ <%# <%= image_tag message.image.variant(resize: '500x500'), class: 'message-image' if message.image.attached? %>
130
140
 
131
141
  </div>
132
142
 
@@ -146,6 +156,8 @@
146
156
 
147
157
  ```javascript
148
158
 
159
+ -- message_channel.js --
160
+
149
161
  import consumer from "./consumer"
150
162
 
151
163
 
@@ -182,8 +194,6 @@
182
194
 
183
195
  if (currentUser == sendUser) {
184
196
 
185
- debugger
186
-
187
197
  const HTML = `
188
198
 
189
199
  <div class="my-content">
@@ -192,7 +202,7 @@
192
202
 
193
203
  <div class="my-message-date">
194
204
 
195
- ${messageData}
205
+ ${messageData}
196
206
 
197
207
  </div>
198
208
 
@@ -218,8 +228,6 @@
218
228
 
219
229
  } else {
220
230
 
221
- debugger
222
-
223
231
  const HTML = `
224
232
 
225
233
  <div class="content">
@@ -274,11 +282,13 @@
274
282
 
275
283
  ```ruby
276
284
 
285
+ -- message_channel.rb --
286
+
277
287
  class MessageChannel < ApplicationCable::Channel
278
288
 
279
289
  def subscribed
280
290
 
281
- stream_from "message_channel"
291
+ stream_from 'message_channel'
282
292
 
283
293
  end
284
294
 
@@ -296,8 +306,52 @@
296
306
 
297
307
 
298
308
 
309
+ ```ruby
310
+
311
+ -- application_cable/connection.rb --
312
+
313
+ module ApplicationCable
314
+
315
+ class Connection < ActionCable::Connection::Base
316
+
317
+ identified_by :current_user
318
+
319
+
320
+
321
+ def connect
322
+
323
+ self.current_user = find_verified_user
324
+
325
+ end
326
+
327
+
328
+
329
+ private
330
+
331
+
332
+
333
+ def find_verified_user
334
+
335
+ if verified_user = User.find_by(id: cookies.signed[:user_id])
336
+
337
+ verified_user
338
+
339
+ else
340
+
341
+ reject_unauthorized_connection
342
+
343
+ end
344
+
345
+ end
346
+
347
+ end
348
+
349
+ end
350
+
351
+ ```
352
+
353
+
354
+
299
- Messages_controllerにて、ActionCableに渡すインスタンスを様々な形に変えてみたり行いましたが、解決できませんでした。
355
+ Messages_controllerにて、ActionCableに渡すインスタンスの変更、connection.rbでcurrent_user定義したり行いましたが、受信者と送信者ビューを変えることができませんでした。
300
-
301
- 冗長になってしまい、申し訳ございません。
356
+
302
-
303
- 恐れ入りますが、どうぞよろしくお願致します。
357
+ 恐れ入りますが、どなたかご教授ただきたいです。

3

gifのリンクの編集

2021/02/22 16:20

投稿

Aki-F
Aki-F

スコア0

test CHANGED
@@ -1 +1 @@
1
- ActionCableでログイン中のユーザーの取得方法知りたいです。
1
+ ActionCableでログイン中のユーザーの取得方法知りたいです。
test CHANGED
@@ -1,8 +1,8 @@
1
1
  ### 解決したいこと
2
2
 
3
- Ruby on RailsのActionCable対話型のラインのようなチャット機能を作成しています。
3
+ Ruby on RailsのActionCableを使用して対話型のラインのようなチャット機能を作成しています。
4
-
4
+
5
- リアルタイムチャット自体は実装できたのですが、別ブラウザ+別アカウントで機能確認を行ったところ、ログイン中のユーザーidをうまく取得できていない様子で、片方で送信したがどちらも送信側の表示になってしまいます。リロードを行うと送信受信それぞれ正常な位置に戻ります。
5
+ リアルタイムチャット自体は実装できたのですが、別ブラウザ+別アカウントで機能確認を行ったところ、ログイン中のユーザーidをうまく取得できていない様子で、片方で送信したメッセがどちらの画面でも送信側の表示になってしまいます。リロードを行うと送信受信それぞれ正常な位置に戻ります。
6
6
 
7
7
  下記Gifのような現象が発生しています。
8
8
 
@@ -12,7 +12,7 @@
12
12
 
13
13
  ### 発生している問題・エラー
14
14
 
15
- https://i.gyazo.com/45f5a2439c852f3abb962ce6ab66ba58.gif
15
+ [リアルタイムチャット画面](https://i.gyazo.com/45f5a2439c852f3abb962ce6ab66ba58.gif)
16
16
 
17
17
 
18
18
 

2

文法の修正

2021/02/20 14:27

投稿

Aki-F
Aki-F

スコア0

test CHANGED
@@ -1 +1 @@
1
- ActionCableでのチャット機能でログイン中のユーザー取得たい
1
+ ActionCableでログイン中のユーザー取得方法を知りたいです。
test CHANGED
@@ -298,4 +298,6 @@
298
298
 
299
299
  Messages_controllerにて、ActionCableに渡すインスタンスを様々な形に変えてみたりと行いましたが、解決できませんでした。
300
300
 
301
- 冗長になってしまい、申し訳ございません。どうぞよろしくお願い致します。
301
+ 冗長になってしまい、申し訳ございません。
302
+
303
+ 恐れ入りますが、どうぞよろしくお願い致します。

1

タイトル編集

2021/02/02 10:31

投稿

Aki-F
Aki-F

スコア0

test CHANGED
@@ -1 +1 @@
1
- ActionCableでのユーザー分岐
1
+ ActionCableでのチャット機能でログイン中のユーザーを取得したい
test CHANGED
File without changes