質問編集履歴

5

_message.html.erb追加

2020/06/02 01:22

投稿

HTMLdoc
HTMLdoc

スコア67

test CHANGED
File without changes
test CHANGED
@@ -40,17 +40,373 @@
40
40
 
41
41
 
42
42
 
43
- room_channel.rb
43
+ _message.html.erb
44
+
45
+ ```erb
46
+
47
+ <div class="message" id="message-<%= message.id %>">
48
+
49
+ <div class="usernameAndTimeBox">
50
+
51
+ <% unless message.user_id.nil? %>
52
+
53
+ <%= link_to message.username, user_path(message.user_id),class: "message_username" %>
54
+
55
+ <% else %>
56
+
57
+ <p class="message_username"><%= message.username %></p>
58
+
59
+ <% end %>
60
+
61
+ <p class="agomessage"><%= time_ago_in_words(message.created_at) + "前" %></p>
62
+
63
+ </div>
64
+
65
+ <% if user_signed_in? %>
66
+
67
+ <% if message.login == true %>
68
+
69
+ <%# if message.user_id == current_user.id %>
70
+
71
+ <button id="<%= message.id %>" class="delete_btn">削除</button>
72
+
73
+ <%# end %>
74
+
75
+ <% end %>
76
+
77
+ <% end %>
78
+
79
+ <%= raw Rinku.auto_link simple_format h(message.content), class: "message_content" %>
80
+
81
+ <% unless message.youtube_id.nil? %>
82
+
83
+ <% iframe = content_tag(
84
+
85
+ :iframe,
86
+
87
+ '',
88
+
89
+ width: 560,
90
+
91
+ height: 315,
92
+
93
+ src: "https://www.YouTube.com/embed/#{message.youtube_id}",
94
+
95
+ frameborder: 0,
96
+
97
+ allowfullscreen: true,
98
+
99
+ class: "youtube-container"
100
+
101
+ ) %>
102
+
103
+ <%= content_tag(:div, iframe, class: "youtube-container2") %>
104
+
105
+ <% end %>
106
+
107
+ <% if message.image? %>
108
+
109
+ <%= image_tag message.image.url %>
110
+
111
+ <% end %>
112
+
113
+ <!-- <div class="editBox">
114
+
115
+ <div class="formchat">
116
+
117
+ <%#= form_with model: message, url: message_path(message) do |f| %>
118
+
119
+ <%#= f.rich_text_area :content %>
120
+
121
+ <%#= f.submit %>
122
+
123
+ <%# end %>
124
+
125
+ </div>
126
+
127
+ </div> -->
128
+
129
+ </div>
130
+
131
+ ```
132
+
133
+
134
+
135
+ message_broadcast_job.rb
44
136
 
45
137
  ```rb
46
138
 
139
+ class MessageBroadcastJob < ApplicationJob
140
+
141
+ queue_as :default
142
+
143
+
144
+
145
+ def perform(message)
146
+
147
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
148
+
149
+ end
150
+
151
+
152
+
153
+ private
154
+
155
+ def render_message(message)
156
+
157
+ ApplicationController.renderer.render partial: 'messages/message', locals: {message: message}
158
+
159
+ end
160
+
161
+ end
162
+
163
+
164
+
165
+ ```
166
+
167
+
168
+
169
+ room_channel.js
170
+
171
+ ```js
172
+
173
+ import consumer from "./consumer"
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(data) {
196
+
197
+ // 0.01秒たったら下に移動。
198
+
199
+ setTimeout(function(){
200
+
201
+ var submitscroll = $('#bottomGO').offset().top;
202
+
203
+ $('html, body').scrollTop(submitscroll);
204
+
205
+ },10);
206
+
207
+ return $('#messages').append(data['message']);
208
+
209
+
210
+
211
+ // Called when there's incoming data on the websocket for this channel
212
+
213
+ },
214
+
215
+
216
+
217
+ speak: function(message) {
218
+
219
+ return this.perform('speak', {
220
+
221
+ message: message
222
+
223
+ });
224
+
225
+ }
226
+
227
+ });
228
+
229
+ $(document).on('keypress submit click', '[data-behavior~=room_speaker]', function(event){
230
+
231
+
232
+
233
+ console.log(event.target.value);
234
+
235
+ function chatgo(){
236
+
237
+ chatChannel.speak(event.target.value);
238
+
239
+ event.target.value = '';
240
+
241
+ return event.preventDefault();
242
+
243
+ }
244
+
245
+ if(event.shiftKey){
246
+
247
+ if(event.keyCode === 13){
248
+
249
+ if(event.target.value != ''){
250
+
251
+ chatgo();
252
+
253
+ }else if(event.target.value == '' || event.target.length <= 1000){
254
+
255
+ event.preventDefault();
256
+
257
+ event.target.value = event.target.value;
258
+
259
+ }
260
+
261
+ }
262
+
263
+ }
264
+
265
+ $('#submit').click(function(){
266
+
267
+ if(event.target.value != ''){
268
+
269
+ chatgo();
270
+
271
+ }else if(event.target.value == '' || event.target.length <= 1000){
272
+
273
+ event.preventDefault();
274
+
275
+ }
276
+
277
+ });
278
+
279
+ });
280
+
281
+ });
282
+
283
+ ```
284
+
285
+ message.rb
286
+
287
+ ```rb
288
+
289
+ class Message < ApplicationRecord
290
+
291
+ belongs_to :user, optional: true
292
+
293
+ belongs_to :room
294
+
295
+ # has_rich_text :content
296
+
297
+ validates :content, length: {maximum: 1000 }
298
+
299
+ after_create_commit { MessageBroadcastJob.perform_later self }
300
+
301
+ mount_uploader :image, ImageUploader
302
+
303
+
304
+
305
+ validate :image_size
306
+
307
+
308
+
309
+ private
310
+
311
+
312
+
313
+ def image_size
314
+
315
+ if image.size > 5.megabytes
316
+
317
+ errors.add(:image, "容量が大きすぎます。5MB未満のファイルにしてください。")
318
+
319
+ end
320
+
321
+ end
322
+
323
+ end
324
+
325
+
326
+
327
+ ```
328
+
329
+
330
+
331
+ 次はメッセージを削除するところです。
332
+
333
+
334
+
335
+ room_delete_channel.js
336
+
337
+ ```js
338
+
339
+ import consumer from "./consumer"
340
+
341
+ $(function(){
342
+
343
+ const deleteChannel = consumer.subscriptions.create({channel: "RoomDeleteChannel", room: $('#messages').data('room_id') }, {
344
+
345
+ connected() {
346
+
347
+ // Called when the subscription is ready for use on the server
348
+
349
+
350
+
351
+ },
352
+
353
+
354
+
355
+ disconnected() {
356
+
357
+ // Called when the subscription has been terminated by the server
358
+
359
+ },
360
+
361
+
362
+
363
+ received(data) {
364
+
365
+ $('#message-' + data['id']).remove();
366
+
367
+ // Called when there's incoming data on the websocket for this channel
368
+
369
+ },
370
+
371
+
372
+
373
+ delete: function(id) {
374
+
375
+ console.log("idGOGOGO")
376
+
377
+ return this.perform('delete', {
378
+
379
+ id: id
380
+
381
+ });
382
+
383
+ }
384
+
385
+ });
386
+
387
+ $(document).on('click', '.delete_btn', function(event){
388
+
389
+ deleteChannel.delete(event.target.id)
390
+
391
+ // return event.preventDefault();
392
+
393
+ });
394
+
395
+ });
396
+
397
+ ```
398
+
399
+
400
+
401
+ room_delete_channel.rb
402
+
403
+ ```ruby
404
+
47
- class RoomChannel < ApplicationCable::Channel
405
+ class RoomDeleteChannel < ApplicationCable::Channel
48
-
49
-
50
406
 
51
407
  def subscribed
52
408
 
53
- stream_from "room_channel_#{params['room']}"
409
+ stream_from "room_delete_channel"
54
410
 
55
411
  end
56
412
 
@@ -64,568 +420,202 @@
64
420
 
65
421
 
66
422
 
67
- def speak(data)
68
-
69
- ActionCable.server.broadcast 'room_channel', room_id: params['room']
70
-
71
- ip = self.connection.ip_addr
72
-
73
- puts "======================" + ip.to_s
74
-
75
- now = Time.now
76
-
77
- secondsAgo = now - 5
78
-
79
- if data['message'].include?("https://www.youtube.com/watch?v=")
80
-
81
- urll = data['message'].gsub(/http.+v=/, "")
82
-
83
- url = urll.gsub(/&.+./, "")
84
-
85
- elsif data['message'].include?("https://youtu.be/")
86
-
87
- url = data['message'].gsub(/http.+be./, "")
88
-
89
- end
90
-
91
- messagesCount = Message.where(ip_id: ip).where('created_at > ?', secondsAgo).count
423
+ def delete(data)
424
+
425
+ ActionCable.server.broadcast 'room_delete_channel', id: data['id']
426
+
427
+ message = Message.find_by(id: data['id'], room_id: params['room'],user_id: current_user.id,login: true)
428
+
429
+ message.destroy
430
+
431
+ end
432
+
433
+ end
434
+
435
+ ```
436
+
437
+
438
+
439
+ current_userとipを扱うためのファイル
440
+
441
+ connection.rb
442
+
443
+ ```rb
444
+
445
+ module ApplicationCable
446
+
447
+ class Connection < ActionCable::Connection::Base
448
+
449
+ identified_by :current_user
450
+
451
+
452
+
453
+   attr_accessor :ip_addr
454
+
455
+ def connect
456
+
457
+ self.current_user = find_verified_user
458
+
459
+    @ip_addr = request.ip
460
+
461
+ end
462
+
463
+
464
+
465
+ private
466
+
467
+
468
+
469
+ def find_verified_user
470
+
471
+ session_key = cookies.encrypted[Rails.application.config.session_options[:key]]
472
+
473
+ if session_key['warden.user.user.key'].present?
474
+
475
+ verified_id = session_key['warden.user.user.key'][0][0]
476
+
477
+ verified_user = User.find_by(id: verified_id)
478
+
479
+ return reject_unauthorized_connection unless verified_user
480
+
481
+ verified_user
482
+
483
+ else
484
+
485
+ return false
486
+
487
+ end
488
+
489
+ end
490
+
491
+ end
492
+
493
+ end
494
+
495
+
496
+
497
+ ```
498
+
499
+
500
+
501
+ __**追記**__
502
+
503
+
504
+
505
+ ActionCableで、メッセージを保存して、
506
+
507
+ ActionJobで、再度
508
+
509
+ ApplicationController.renderer.render
510
+
511
+
512
+
513
+ を使って、読み込むのですが、
514
+
515
+ 読み込んだ先のファイルで
516
+
517
+ user_signed_in? と current_userなどの deiviseが用意しているヘルパーメソッドがエラーで使えなくなりました。
518
+
519
+
520
+
521
+ 調べたらApplicationController.renderer.renderはenvに直接アクセスすることができないみたいです。
522
+
523
+
524
+
525
+
526
+
527
+ [https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/](https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/)
528
+
529
+
530
+
531
+ このサイトを参考にして、やってみましたが、うまくいかず
532
+
533
+ ```
534
+
535
+ ActionView::MissingTemplate (Missing template messages/message with {:locale=>[:ja], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
536
+
537
+ * "/home/vagrant/chatapp/app/views"
538
+
539
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/app/views"
540
+
541
+ *
542
+
543
+ "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actiontext-6.0.3/app/views"
544
+
545
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3/app/views"
546
+
547
+ ):
548
+
549
+ ```
550
+
551
+ が出てしまいました。。。
552
+
553
+
554
+
555
+ 修正してみたファイルを記述します。
556
+
557
+ message_broadcast_job.rb
558
+
559
+ ```rb
560
+
561
+ class MessageBroadcastJob < ApplicationJob
562
+
563
+ queue_as :default
564
+
565
+
566
+
567
+ def perform(message)
568
+
569
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
570
+
571
+ end
572
+
573
+ private
574
+
575
+
576
+
577
+ def render_message(message)
92
578
 
93
579
 
94
580
 
95
- if current_user == false
96
-
97
- if messagesCount <= 10
98
-
99
- unless data['message'].nil?
581
+ # ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
100
-
582
+
101
- if Usermanager.where(ip_id: ip, room_ban: false,message_limit: false, room_id: params['room'], login: false).exists?
583
+ ApplicationController.render_with_signed_in_user(message.user_id, 'messages/message', locals: { message: message })
102
-
103
- if data['message'].length <= 1000
584
+
104
-
105
- Message.create! content: data['message'], room_id: params['room'],username: "名無し",ip_id: ip, login: false, youtube_id: url
106
-
107
- end
585
+ end
108
-
109
- end
586
+
110
-
111
- end
587
+
112
-
113
- end
114
-
115
- else
116
-
117
- if messagesCount <= 10
118
-
119
- unless data['message'].nil?
120
-
121
- if Usermanager.where(user_id: current_user.id,room_ban: false, room_id: params['room'], message_limit: false, login: true).exists?
122
-
123
- if data['message'].length <= 1000
124
-
125
- Message.create! content: data['message'], room_id: params['room'],username: current_user.name, ip_id: ip, login: true, user_id: current_user.id, youtube_id: url
126
-
127
- end
128
-
129
- end
130
-
131
- end
132
-
133
- end
134
-
135
- end
136
-
137
- end
138
588
 
139
589
  end
140
590
 
591
+
592
+
141
- ```
593
+ ```
142
-
143
-
144
-
594
+
595
+
596
+
145
- message_broadcast_job.rb
597
+ application.controller.rb
146
598
 
147
599
  ```rb
148
600
 
601
+ ...
602
+
603
+ def self.render_with_signed_in_user(user, *args)
604
+
605
+ ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
606
+
607
+ proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap{|i| i.set_user(user, scope: :user) }
608
+
609
+ renderer = self.renderer.new('warden' => proxy)
610
+
149
- class MessageBroadcastJob < ApplicationJob
611
+ puts "====================" + renderer.to_s
150
-
151
- queue_as :default
612
+
152
-
153
-
154
-
155
- def perform(message)
613
+ renderer.render(*args)
156
-
157
- ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
614
+
158
-
159
- end
615
+ end
160
-
161
-
162
-
163
- private
164
-
165
- def render_message(message)
166
-
167
- ApplicationController.renderer.render partial: 'messages/message', locals: {message: message}
168
-
169
- end
170
616
 
171
617
  end
172
618
 
173
619
 
174
620
 
175
621
  ```
176
-
177
-
178
-
179
- room_channel.js
180
-
181
- ```js
182
-
183
- import consumer from "./consumer"
184
-
185
- $(function(){
186
-
187
- const chatChannel = consumer.subscriptions.create({channel: "RoomChannel", room: $('#messages').data('room_id') }, {
188
-
189
- connected() {
190
-
191
- // Called when the subscription is ready for use on the server
192
-
193
- },
194
-
195
-
196
-
197
- disconnected() {
198
-
199
- // Called when the subscription has been terminated by the server
200
-
201
- },
202
-
203
-
204
-
205
- received(data) {
206
-
207
- // 0.01秒たったら下に移動。
208
-
209
- setTimeout(function(){
210
-
211
- var submitscroll = $('#bottomGO').offset().top;
212
-
213
- $('html, body').scrollTop(submitscroll);
214
-
215
- },10);
216
-
217
- return $('#messages').append(data['message']);
218
-
219
-
220
-
221
- // Called when there's incoming data on the websocket for this channel
222
-
223
- },
224
-
225
-
226
-
227
- speak: function(message) {
228
-
229
- return this.perform('speak', {
230
-
231
- message: message
232
-
233
- });
234
-
235
- }
236
-
237
- });
238
-
239
- $(document).on('keypress submit click', '[data-behavior~=room_speaker]', function(event){
240
-
241
-
242
-
243
- console.log(event.target.value);
244
-
245
- function chatgo(){
246
-
247
- chatChannel.speak(event.target.value);
248
-
249
- event.target.value = '';
250
-
251
- return event.preventDefault();
252
-
253
- }
254
-
255
- if(event.shiftKey){
256
-
257
- if(event.keyCode === 13){
258
-
259
- if(event.target.value != ''){
260
-
261
- chatgo();
262
-
263
- }else if(event.target.value == '' || event.target.length <= 1000){
264
-
265
- event.preventDefault();
266
-
267
- event.target.value = event.target.value;
268
-
269
- }
270
-
271
- }
272
-
273
- }
274
-
275
- $('#submit').click(function(){
276
-
277
- if(event.target.value != ''){
278
-
279
- chatgo();
280
-
281
- }else if(event.target.value == '' || event.target.length <= 1000){
282
-
283
- event.preventDefault();
284
-
285
- }
286
-
287
- });
288
-
289
- });
290
-
291
- });
292
-
293
- ```
294
-
295
- message.rb
296
-
297
- ```rb
298
-
299
- class Message < ApplicationRecord
300
-
301
- belongs_to :user, optional: true
302
-
303
- belongs_to :room
304
-
305
- # has_rich_text :content
306
-
307
- validates :content, length: {maximum: 1000 }
308
-
309
- after_create_commit { MessageBroadcastJob.perform_later self }
310
-
311
- mount_uploader :image, ImageUploader
312
-
313
-
314
-
315
- validate :image_size
316
-
317
-
318
-
319
- private
320
-
321
-
322
-
323
- def image_size
324
-
325
- if image.size > 5.megabytes
326
-
327
- errors.add(:image, "容量が大きすぎます。5MB未満のファイルにしてください。")
328
-
329
- end
330
-
331
- end
332
-
333
- end
334
-
335
-
336
-
337
- ```
338
-
339
-
340
-
341
- 次はメッセージを削除するところです。
342
-
343
-
344
-
345
- room_delete_channel.js
346
-
347
- ```js
348
-
349
- import consumer from "./consumer"
350
-
351
- $(function(){
352
-
353
- const deleteChannel = consumer.subscriptions.create({channel: "RoomDeleteChannel", room: $('#messages').data('room_id') }, {
354
-
355
- connected() {
356
-
357
- // Called when the subscription is ready for use on the server
358
-
359
-
360
-
361
- },
362
-
363
-
364
-
365
- disconnected() {
366
-
367
- // Called when the subscription has been terminated by the server
368
-
369
- },
370
-
371
-
372
-
373
- received(data) {
374
-
375
- $('#message-' + data['id']).remove();
376
-
377
- // Called when there's incoming data on the websocket for this channel
378
-
379
- },
380
-
381
-
382
-
383
- delete: function(id) {
384
-
385
- console.log("idGOGOGO")
386
-
387
- return this.perform('delete', {
388
-
389
- id: id
390
-
391
- });
392
-
393
- }
394
-
395
- });
396
-
397
- $(document).on('click', '.delete_btn', function(event){
398
-
399
- deleteChannel.delete(event.target.id)
400
-
401
- // return event.preventDefault();
402
-
403
- });
404
-
405
- });
406
-
407
- ```
408
-
409
-
410
-
411
- room_delete_channel.rb
412
-
413
- ```ruby
414
-
415
- class RoomDeleteChannel < ApplicationCable::Channel
416
-
417
- def subscribed
418
-
419
- stream_from "room_delete_channel"
420
-
421
- end
422
-
423
-
424
-
425
- def unsubscribed
426
-
427
- # Any cleanup needed when channel is unsubscribed
428
-
429
- end
430
-
431
-
432
-
433
- def delete(data)
434
-
435
- ActionCable.server.broadcast 'room_delete_channel', id: data['id']
436
-
437
- message = Message.find_by(id: data['id'], room_id: params['room'],user_id: current_user.id,login: true)
438
-
439
- message.destroy
440
-
441
- end
442
-
443
- end
444
-
445
- ```
446
-
447
-
448
-
449
- current_userとipを扱うためのファイル
450
-
451
- connection.rb
452
-
453
- ```rb
454
-
455
- module ApplicationCable
456
-
457
- class Connection < ActionCable::Connection::Base
458
-
459
- identified_by :current_user
460
-
461
-
462
-
463
-   attr_accessor :ip_addr
464
-
465
- def connect
466
-
467
- self.current_user = find_verified_user
468
-
469
-    @ip_addr = request.ip
470
-
471
- end
472
-
473
-
474
-
475
- private
476
-
477
-
478
-
479
- def find_verified_user
480
-
481
- session_key = cookies.encrypted[Rails.application.config.session_options[:key]]
482
-
483
- if session_key['warden.user.user.key'].present?
484
-
485
- verified_id = session_key['warden.user.user.key'][0][0]
486
-
487
- verified_user = User.find_by(id: verified_id)
488
-
489
- return reject_unauthorized_connection unless verified_user
490
-
491
- verified_user
492
-
493
- else
494
-
495
- return false
496
-
497
- end
498
-
499
- end
500
-
501
- end
502
-
503
- end
504
-
505
-
506
-
507
- ```
508
-
509
-
510
-
511
- __**追記**__
512
-
513
-
514
-
515
- ActionCableで、メッセージを保存して、
516
-
517
- ActionJobで、再度
518
-
519
- ApplicationController.renderer.render
520
-
521
-
522
-
523
- を使って、読み込むのですが、
524
-
525
- 読み込んだ先のファイルで
526
-
527
- user_signed_in? と current_userなどの deiviseが用意しているヘルパーメソッドがエラーで使えなくなりました。
528
-
529
-
530
-
531
- 調べたらApplicationController.renderer.renderはenvに直接アクセスすることができないみたいです。
532
-
533
-
534
-
535
-
536
-
537
- [https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/](https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/)
538
-
539
-
540
-
541
- このサイトを参考にして、やってみましたが、うまくいかず
542
-
543
- ```
544
-
545
- ActionView::MissingTemplate (Missing template messages/message with {:locale=>[:ja], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
546
-
547
- * "/home/vagrant/chatapp/app/views"
548
-
549
- * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/app/views"
550
-
551
- *
552
-
553
- "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actiontext-6.0.3/app/views"
554
-
555
- * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3/app/views"
556
-
557
- ):
558
-
559
- ```
560
-
561
- が出てしまいました。。。
562
-
563
-
564
-
565
- 修正してみたファイルを記述します。
566
-
567
- message_broadcast_job.rb
568
-
569
- ```rb
570
-
571
- class MessageBroadcastJob < ApplicationJob
572
-
573
- queue_as :default
574
-
575
-
576
-
577
- def perform(message)
578
-
579
- ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
580
-
581
- end
582
-
583
- private
584
-
585
-
586
-
587
- def render_message(message)
588
-
589
-
590
-
591
- # ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
592
-
593
- ApplicationController.render_with_signed_in_user(message.user_id, 'messages/message', locals: { message: message })
594
-
595
- end
596
-
597
-
598
-
599
- end
600
-
601
-
602
-
603
- ```
604
-
605
-
606
-
607
- application.controller.rb
608
-
609
- ```rb
610
-
611
- ...
612
-
613
- def self.render_with_signed_in_user(user, *args)
614
-
615
- ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
616
-
617
- proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap{|i| i.set_user(user, scope: :user) }
618
-
619
- renderer = self.renderer.new('warden' => proxy)
620
-
621
- puts "====================" + renderer.to_s
622
-
623
- renderer.render(*args)
624
-
625
- end
626
-
627
- end
628
-
629
-
630
-
631
- ```

4

file追加

2020/06/02 01:22

投稿

HTMLdoc
HTMLdoc

スコア67

test CHANGED
File without changes
test CHANGED
@@ -559,3 +559,73 @@
559
559
  ```
560
560
 
561
561
  が出てしまいました。。。
562
+
563
+
564
+
565
+ 修正してみたファイルを記述します。
566
+
567
+ message_broadcast_job.rb
568
+
569
+ ```rb
570
+
571
+ class MessageBroadcastJob < ApplicationJob
572
+
573
+ queue_as :default
574
+
575
+
576
+
577
+ def perform(message)
578
+
579
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
580
+
581
+ end
582
+
583
+ private
584
+
585
+
586
+
587
+ def render_message(message)
588
+
589
+
590
+
591
+ # ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
592
+
593
+ ApplicationController.render_with_signed_in_user(message.user_id, 'messages/message', locals: { message: message })
594
+
595
+ end
596
+
597
+
598
+
599
+ end
600
+
601
+
602
+
603
+ ```
604
+
605
+
606
+
607
+ application.controller.rb
608
+
609
+ ```rb
610
+
611
+ ...
612
+
613
+ def self.render_with_signed_in_user(user, *args)
614
+
615
+ ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
616
+
617
+ proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap{|i| i.set_user(user, scope: :user) }
618
+
619
+ renderer = self.renderer.new('warden' => proxy)
620
+
621
+ puts "====================" + renderer.to_s
622
+
623
+ renderer.render(*args)
624
+
625
+ end
626
+
627
+ end
628
+
629
+
630
+
631
+ ```

3

追記した。 調べたことを

2020/06/02 01:19

投稿

HTMLdoc
HTMLdoc

スコア67

test CHANGED
File without changes
test CHANGED
@@ -505,3 +505,57 @@
505
505
 
506
506
 
507
507
  ```
508
+
509
+
510
+
511
+ __**追記**__
512
+
513
+
514
+
515
+ ActionCableで、メッセージを保存して、
516
+
517
+ ActionJobで、再度
518
+
519
+ ApplicationController.renderer.render
520
+
521
+
522
+
523
+ を使って、読み込むのですが、
524
+
525
+ 読み込んだ先のファイルで
526
+
527
+ user_signed_in? と current_userなどの deiviseが用意しているヘルパーメソッドがエラーで使えなくなりました。
528
+
529
+
530
+
531
+ 調べたらApplicationController.renderer.renderはenvに直接アクセスすることができないみたいです。
532
+
533
+
534
+
535
+
536
+
537
+ [https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/](https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/)
538
+
539
+
540
+
541
+ このサイトを参考にして、やってみましたが、うまくいかず
542
+
543
+ ```
544
+
545
+ ActionView::MissingTemplate (Missing template messages/message with {:locale=>[:ja], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
546
+
547
+ * "/home/vagrant/chatapp/app/views"
548
+
549
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/app/views"
550
+
551
+ *
552
+
553
+ "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actiontext-6.0.3/app/views"
554
+
555
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3/app/views"
556
+
557
+ ):
558
+
559
+ ```
560
+
561
+ が出てしまいました。。。

2

message.rb追加

2020/06/02 01:17

投稿

HTMLdoc
HTMLdoc

スコア67

test CHANGED
File without changes
test CHANGED
@@ -292,6 +292,50 @@
292
292
 
293
293
  ```
294
294
 
295
+ message.rb
296
+
297
+ ```rb
298
+
299
+ class Message < ApplicationRecord
300
+
301
+ belongs_to :user, optional: true
302
+
303
+ belongs_to :room
304
+
305
+ # has_rich_text :content
306
+
307
+ validates :content, length: {maximum: 1000 }
308
+
309
+ after_create_commit { MessageBroadcastJob.perform_later self }
310
+
311
+ mount_uploader :image, ImageUploader
312
+
313
+
314
+
315
+ validate :image_size
316
+
317
+
318
+
319
+ private
320
+
321
+
322
+
323
+ def image_size
324
+
325
+ if image.size > 5.megabytes
326
+
327
+ errors.add(:image, "容量が大きすぎます。5MB未満のファイルにしてください。")
328
+
329
+ end
330
+
331
+ end
332
+
333
+ end
334
+
335
+
336
+
337
+ ```
338
+
295
339
 
296
340
 
297
341
  次はメッセージを削除するところです。

1

インデントそろえたり いらないコメント削除

2020/05/31 06:27

投稿

HTMLdoc
HTMLdoc

スコア67

test CHANGED
@@ -1 +1 @@
1
- railsのActionCableを使ったチャットアプリでエラー
1
+ railsのActionCableを使ったチャットアプリでエラー ActionView::Template::Error (Devise could not find the `Warden::Pro
test CHANGED
@@ -328,8 +328,6 @@
328
328
 
329
329
  received(data) {
330
330
 
331
- // 0.01秒たったら下に移動。
332
-
333
331
  $('#message-' + data['id']).remove();
334
332
 
335
333
  // Called when there's incoming data on the websocket for this channel
@@ -418,13 +416,13 @@
418
416
 
419
417
 
420
418
 
421
- attr_accessor :ip_addr
419
+   attr_accessor :ip_addr
422
420
 
423
421
  def connect
424
422
 
425
423
  self.current_user = find_verified_user
426
424
 
427
- @ip_addr = request.ip
425
+    @ip_addr = request.ip
428
426
 
429
427
  end
430
428