質問編集履歴

1

画像をテキストに変えました

2021/02/07 15:17

投稿

tatsuhiros
tatsuhiros

スコア0

test CHANGED
File without changes
test CHANGED
@@ -6,28 +6,310 @@
6
6
 
7
7
  もし分かる方がいれば、お力をお借りしたいです。
8
8
 
9
- 以下のようなコードとなっています。
10
-
11
-
12
-
13
- ![room_channel.js](ba6531fe1fcae179da337bafb6ef2838.png)
14
-
15
- ![room_channel.js](9f634b72d4e9c7a567b3463bbda1771a.png)](597fb33f553351c522ac34090a1f0398.png)
16
-
17
-
18
-
19
- ![messages_controller.rb](19549ca88412a213279d2399ccc05a71.png)
20
-
21
- ![_message.html.erb](fac8b8923abea70465b66add376e91ae.png)
22
-
23
- ![message.rb](d9610b54cb73ed678ccbb64f5800095b.png)
24
-
25
- ![message_broadcast_job.rb](26e45fedf10c44c9d6d3bc70cbe6dcc1.png)
26
-
27
- ![rooms/show.html.erb](fc3051372492ba6045bbed15adfa3f1c.png)
28
-
29
9
 
30
10
 
31
11
  ずっと困っているので、助けていただければ本当に助かります!
32
12
 
33
13
  どうか宜しくお願いいたします。
14
+
15
+
16
+
17
+ 以下のようなコードとなっております。
18
+
19
+
20
+
21
+ ```ruby
22
+
23
+ class RoomChannel < ApplicationCable::Channel
24
+
25
+ def subscribed
26
+
27
+ stream_from "room_channel_#{params['room']}"
28
+
29
+ end
30
+
31
+
32
+
33
+ def unsubscribed
34
+
35
+ # Any cleanup needed when channel is unsubscribed
36
+
37
+ end
38
+
39
+
40
+
41
+ def speak(data)
42
+
43
+ # jsで実行されたspeakのmessageを受け取り、room_channelのreceivedにブロードキャストする
44
+
45
+ Message.create! content: data['message'], user_id: current_user.id, room_id: params['room']
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+
53
+ ```
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+ ```ruby
62
+
63
+ class MessageBroadcastJob < ApplicationJob
64
+
65
+ queue_as :default
66
+
67
+
68
+
69
+ def perform(message)
70
+
71
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
72
+
73
+ end
74
+
75
+
76
+
77
+ private
78
+
79
+
80
+
81
+ def render_message(message)
82
+
83
+ ApplicationController.render_with_signed_in_user(message.user, partial: 'messages/message', locals: { message: message })
84
+
85
+ end
86
+
87
+ end
88
+
89
+ ```
90
+
91
+ ```Ruby
92
+
93
+ module ApplicationCable
94
+
95
+ class Connection < ActionCable::Connection::Base
96
+
97
+ identified_by :current_user
98
+
99
+
100
+
101
+ def connect
102
+
103
+ reject_unauthorized_connection unless find_verified_user
104
+
105
+ end
106
+
107
+
108
+
109
+ private
110
+
111
+
112
+
113
+ def find_verified_user
114
+
115
+ self.current_user = env['warden'].user
116
+
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ ```
124
+
125
+
126
+
127
+ ```Ruby
128
+
129
+ class Message < ApplicationRecord
130
+
131
+ validates :content, presence: true
132
+
133
+ after_create_commit { MessageBroadcastJob.perform_later self }
134
+
135
+ belongs_to :user
136
+
137
+ belongs_to :room
138
+
139
+ has_many :active_notifications, class_name: 'Notification', foreign_key: 'visitor_id', dependent: :destroy
140
+
141
+ has_many :passive_notifications, class_name: 'Notification', foreign_key: 'visited_id', dependent: :destroy
142
+
143
+ end
144
+
145
+
146
+
147
+ ```
148
+
149
+
150
+
151
+ ```Ruby
152
+
153
+ class MessagesController < ApplicationController
154
+
155
+ end
156
+
157
+
158
+
159
+ ```
160
+
161
+
162
+
163
+ ```JavaScript
164
+
165
+ import consumer from './consumer'
166
+
167
+
168
+
169
+ // $(function() { ... }); で囲むことでレンダリング後に実行される
170
+
171
+ // レンダリング前に実行されると $('#messages').data('room_id') が取得できない
172
+
173
+ // turbolinks を使っている場合は $(document).on('turbolinks:load', function() { ... }); で囲う
174
+
175
+ $(function() {
176
+
177
+ const chatChannel = consumer.subscriptions.create({ channel: 'RoomChannel', room: $('#messages').data('room_id') }, {
178
+
179
+ connected() {
180
+
181
+ // Called when the subscription is ready for use on the server
182
+
183
+ },
184
+
185
+
186
+
187
+ disconnected() {
188
+
189
+ // Called when the subscription has been terminated by the server
190
+
191
+ },
192
+
193
+
194
+
195
+ received: function(data) {
196
+
197
+ return $('#messages').append(data['message']);
198
+
199
+ },
200
+
201
+
202
+
203
+ speak: function(message) {
204
+
205
+ return this.perform('speak', {
206
+
207
+ message: message
208
+
209
+ });
210
+
211
+ }
212
+
213
+ });
214
+
215
+
216
+
217
+ $(document).on('keypress', '[data-behavior~=room_speaker]', function(event) {
218
+
219
+ if (event.keyCode === 13) {
220
+
221
+ chatChannel.speak(event.target.value);
222
+
223
+ event.target.value = '';
224
+
225
+ return event.preventDefault();
226
+
227
+ }
228
+
229
+ });
230
+
231
+
232
+
233
+ $(".button").on("click",function(){
234
+
235
+ var message = $(".speaker_txt").val();
236
+
237
+ chatChannel.speak(message);
238
+
239
+ $(".speaker_txt").val("")
240
+
241
+ });
242
+
243
+ });
244
+
245
+
246
+
247
+ ```
248
+
249
+
250
+
251
+
252
+
253
+ ```HTML
254
+
255
+ <div class="chat-room-container">
256
+
257
+ <%= link_to rooms_path do%>
258
+
259
+ <i class="fas fa-arrow-left fa-2x" aria-hidden="true"></i>
260
+
261
+ <% end %>
262
+
263
+ <% if @user.image? %>
264
+
265
+ <%= link_to user_path(@user) do %>
266
+
267
+ <%= image_tag(@user.image.to_s, :class => "top-image")%>
268
+
269
+ <% end %>
270
+
271
+ <span class="name"><%= @user.name %></span>
272
+
273
+ <% else %>
274
+
275
+ <%= link_to user_path(@user) do %>
276
+
277
+ <%= image_tag("default.png", :class => "top-image")%>
278
+
279
+ <% end %>
280
+
281
+ <span class="name"><%= @user.name %></span>
282
+
283
+ <% end %>
284
+
285
+ <hr>
286
+
287
+ <div class= "messages" id ='messages' data-room_id="<%= @room.id %>">
288
+
289
+ <%= render @messages %>
290
+
291
+ </div>
292
+
293
+ </div>
294
+
295
+ <div class= "message-form row">
296
+
297
+ <div class = "send-form offset-1 col-9">
298
+
299
+ <form>
300
+
301
+ <input class="speaker_txt col-12" name="content" type="text" data-behavior="room_speaker", autofocus>
302
+
303
+ </form>
304
+
305
+ </div>
306
+
307
+ <div class= "send-button col-1">
308
+
309
+ <button class="button btn btn-primary"><i class="fas fa-paper-plane"></i></button>
310
+
311
+ </div>
312
+
313
+ </div>
314
+
315
+ ```