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

質問編集履歴

5

_message.html.erb追加

2020/06/02 01:22

投稿

HTMLdoc
HTMLdoc

スコア67

title CHANGED
File without changes
body CHANGED
@@ -19,55 +19,50 @@
19
19
 
20
20
  コードを載せておきます。
21
21
 
22
- room_channel.rb
22
+ _message.html.erb
23
- ```rb
23
+ ```erb
24
- class RoomChannel < ApplicationCable::Channel
25
-
26
- def subscribed
27
- stream_from "room_channel_#{params['room']}"
28
- end
29
-
30
- def unsubscribed
31
- # Any cleanup needed when channel is unsubscribed
32
- end
33
-
34
- def speak(data)
35
- ActionCable.server.broadcast 'room_channel', room_id: params['room']
36
- ip = self.connection.ip_addr
37
- puts "======================" + ip.to_s
38
- now = Time.now
39
- secondsAgo = now - 5
40
- if data['message'].include?("https://www.youtube.com/watch?v=")
41
- urll = data['message'].gsub(/http.+v=/, "")
42
- url = urll.gsub(/&.+./, "")
43
- elsif data['message'].include?("https://youtu.be/")
24
+ <div class="message" id="message-<%= message.id %>">
25
+ <div class="usernameAndTimeBox">
26
+ <% unless message.user_id.nil? %>
27
+ <%= link_to message.username, user_path(message.user_id),class: "message_username" %>
28
+ <% else %>
29
+ <p class="message_username"><%= message.username %></p>
30
+ <% end %>
31
+ <p class="agomessage"><%= time_ago_in_words(message.created_at) + "前" %></p>
32
+ </div>
33
+ <% if user_signed_in? %>
34
+ <% if message.login == true %>
35
+ <%# if message.user_id == current_user.id %>
36
+ <button id="<%= message.id %>" class="delete_btn">削除</button>
37
+ <%# end %>
38
+ <% end %>
39
+ <% end %>
40
+ <%= raw Rinku.auto_link simple_format h(message.content), class: "message_content" %>
41
+ <% unless message.youtube_id.nil? %>
42
+ <% iframe = content_tag(
43
+ :iframe,
44
+ '',
45
+ width: 560,
46
+ height: 315,
44
- url = data['message'].gsub(/http.+be./, "")
47
+ src: "https://www.YouTube.com/embed/#{message.youtube_id}",
48
+ frameborder: 0,
49
+ allowfullscreen: true,
50
+ class: "youtube-container"
45
- end
51
+ ) %>
46
- messagesCount = Message.where(ip_id: ip).where('created_at > ?', secondsAgo).count
52
+ <%= content_tag(:div, iframe, class: "youtube-container2") %>
47
-
48
- if current_user == false
53
+ <% end %>
49
- if messagesCount <= 10
54
+ <% if message.image? %>
50
- unless data['message'].nil?
55
+ <%= image_tag message.image.url %>
51
- if Usermanager.where(ip_id: ip, room_ban: false,message_limit: false, room_id: params['room'], login: false).exists?
56
+ <% end %>
52
- if data['message'].length <= 1000
57
+ <!-- <div class="editBox">
58
+ <div class="formchat">
53
- Message.create! content: data['message'], room_id: params['room'],username: "名無し",ip_id: ip, login: false, youtube_id: url
59
+ <%#= form_with model: message, url: message_path(message) do |f| %>
54
- end
55
- end
56
- end
57
- end
58
- else
60
+ <%#= f.rich_text_area :content %>
59
- if messagesCount <= 10
61
+ <%#= f.submit %>
62
+ <%# end %>
63
+ </div>
60
- unless data['message'].nil?
64
+ </div> -->
61
- if Usermanager.where(user_id: current_user.id,room_ban: false, room_id: params['room'], message_limit: false, login: true).exists?
62
- if data['message'].length <= 1000
65
+ </div>
63
- 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
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
70
- end
71
66
  ```
72
67
 
73
68
  message_broadcast_job.rb

4

file追加

2020/06/02 01:22

投稿

HTMLdoc
HTMLdoc

スコア67

title CHANGED
File without changes
body CHANGED
@@ -278,4 +278,39 @@
278
278
  * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3/app/views"
279
279
  ):
280
280
  ```
281
- が出てしまいました。。。
281
+ が出てしまいました。。。
282
+
283
+ 修正してみたファイルを記述します。
284
+ message_broadcast_job.rb
285
+ ```rb
286
+ class MessageBroadcastJob < ApplicationJob
287
+ queue_as :default
288
+
289
+ def perform(message)
290
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
291
+ end
292
+ private
293
+
294
+ def render_message(message)
295
+
296
+ # ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
297
+ ApplicationController.render_with_signed_in_user(message.user_id, 'messages/message', locals: { message: message })
298
+ end
299
+
300
+ end
301
+
302
+ ```
303
+
304
+ application.controller.rb
305
+ ```rb
306
+ ...
307
+ def self.render_with_signed_in_user(user, *args)
308
+ ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
309
+ proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap{|i| i.set_user(user, scope: :user) }
310
+ renderer = self.renderer.new('warden' => proxy)
311
+ puts "====================" + renderer.to_s
312
+ renderer.render(*args)
313
+ end
314
+ end
315
+
316
+ ```

3

追記した。 調べたことを

2020/06/02 01:19

投稿

HTMLdoc
HTMLdoc

スコア67

title CHANGED
File without changes
body CHANGED
@@ -251,4 +251,31 @@
251
251
  end
252
252
  end
253
253
 
254
- ```
254
+ ```
255
+
256
+ __**追記**__
257
+
258
+ ActionCableで、メッセージを保存して、
259
+ ActionJobで、再度
260
+ ApplicationController.renderer.render
261
+
262
+ を使って、読み込むのですが、
263
+ 読み込んだ先のファイルで
264
+ user_signed_in? と current_userなどの deiviseが用意しているヘルパーメソッドがエラーで使えなくなりました。
265
+
266
+ 調べたらApplicationController.renderer.renderはenvに直接アクセスすることができないみたいです。
267
+
268
+
269
+ [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/)
270
+
271
+ このサイトを参考にして、やってみましたが、うまくいかず
272
+ ```
273
+ 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:
274
+ * "/home/vagrant/chatapp/app/views"
275
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/app/views"
276
+ *
277
+ "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actiontext-6.0.3/app/views"
278
+ * "/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/actionmailbox-6.0.3/app/views"
279
+ ):
280
+ ```
281
+ が出てしまいました。。。

2

message.rb追加

2020/06/02 01:17

投稿

HTMLdoc
HTMLdoc

スコア67

title CHANGED
File without changes
body CHANGED
@@ -145,7 +145,29 @@
145
145
  });
146
146
  });
147
147
  ```
148
+ message.rb
149
+ ```rb
150
+ class Message < ApplicationRecord
151
+ belongs_to :user, optional: true
152
+ belongs_to :room
153
+ # has_rich_text :content
154
+ validates :content, length: {maximum: 1000 }
155
+ after_create_commit { MessageBroadcastJob.perform_later self }
156
+ mount_uploader :image, ImageUploader
148
157
 
158
+ validate :image_size
159
+
160
+ private
161
+
162
+ def image_size
163
+ if image.size > 5.megabytes
164
+ errors.add(:image, "容量が大きすぎます。5MB未満のファイルにしてください。")
165
+ end
166
+ end
167
+ end
168
+
169
+ ```
170
+
149
171
  次はメッセージを削除するところです。
150
172
 
151
173
  room_delete_channel.js

1

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

2020/05/31 06:27

投稿

HTMLdoc
HTMLdoc

スコア67

title CHANGED
@@ -1,1 +1,1 @@
1
- railsのActionCableを使ったチャットアプリでエラー
1
+ railsのActionCableを使ったチャットアプリでエラー ActionView::Template::Error (Devise could not find the `Warden::Pro
body CHANGED
@@ -163,7 +163,6 @@
163
163
  },
164
164
 
165
165
  received(data) {
166
- // 0.01秒たったら下に移動。
167
166
  $('#message-' + data['id']).remove();
168
167
  // Called when there's incoming data on the websocket for this channel
169
168
  },
@@ -208,10 +207,10 @@
208
207
  class Connection < ActionCable::Connection::Base
209
208
  identified_by :current_user
210
209
 
211
- attr_accessor :ip_addr
210
+   attr_accessor :ip_addr
212
211
  def connect
213
212
  self.current_user = find_verified_user
214
- @ip_addr = request.ip
213
+    @ip_addr = request.ip
215
214
  end
216
215
 
217
216
  private