質問編集履歴
2
題名の改正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ActionCable リアルタイムチャットの実装
|
1
|
+
ActionCable リアルタイムチャットの実装 入力フォームからの送信ができない
|
test
CHANGED
File without changes
|
1
文法の改正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
ruby on rails でチャット機能を作っています。
|
4
4
|
|
5
|
-
javaのコンソールで```App.room.speak("test")```とうてば
|
5
|
+
javaのコンソールで```App.room.speak("test")```とうてば```test```と表示されるようになったのですが、フォームから送信するときに、一度javaのコンソールで先ほどのようにメッセージを送らないとフォームから文字を送信することができません。更新ボタンなどを押してから、フォームからの送信を行っても何も起きません。
|
6
|
+
|
7
|
+
なんでも良いので気付かれた点や気になった点がございましたら、アドバイスをよろしくお願いします。
|
6
8
|
|
7
9
|
### 該当のソースコード
|
8
10
|
|
@@ -26,9 +28,9 @@
|
|
26
28
|
|
27
29
|
received: (data) ->
|
28
30
|
|
29
|
-
alert data['message']
|
31
|
+
#alert data['message']
|
30
|
-
|
32
|
+
|
31
|
-
|
33
|
+
$('#messages').append data['message']
|
32
34
|
|
33
35
|
# Called when there's incoming data on the websocket for this channel
|
34
36
|
|
@@ -62,6 +64,8 @@
|
|
62
64
|
|
63
65
|
stream_from "room_channel"
|
64
66
|
|
67
|
+
ActionCable.server.broadcast 'room_channel', message: data['message']
|
68
|
+
|
65
69
|
end
|
66
70
|
|
67
71
|
|
@@ -154,49 +158,7 @@
|
|
154
158
|
|
155
159
|
```
|
156
160
|
|
157
|
-
|
161
|
+
|
158
|
-
|
159
|
-
App.room = App.cable.subscriptions.create "RoomChannel",
|
160
|
-
|
161
|
-
connected: ->
|
162
|
-
|
163
|
-
# Called when the subscription is ready for use on the server
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
disconnected: ->
|
168
|
-
|
169
|
-
# Called when the subscription has been terminated by the server
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
received: (data) ->
|
174
|
-
|
175
|
-
alert data['message']
|
176
|
-
|
177
|
-
#$('#messages').append data['message']
|
178
|
-
|
179
|
-
# Called when there's incoming data on the websocket for this channel
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
speak: (message) ->
|
184
|
-
|
185
|
-
@perform 'speak', message: message
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
$ (document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
|
190
|
-
|
191
|
-
if event.keyCode is 13 # return = send
|
192
|
-
|
193
|
-
App.room.speak event.target.value
|
194
|
-
|
195
|
-
event.target.value = ''
|
196
|
-
|
197
|
-
event.preventDefault()
|
198
|
-
|
199
|
-
```
|
200
162
|
|
201
163
|
```
|
202
164
|
|
@@ -246,6 +208,20 @@
|
|
246
208
|
|
247
209
|
```
|
248
210
|
|
211
|
+
```
|
212
|
+
|
213
|
+
//models/message.rb
|
214
|
+
|
215
|
+
class Message < ApplicationRecord
|
216
|
+
|
217
|
+
after_create_commit { MessageBroadcastJob.perform_later self }
|
218
|
+
|
219
|
+
validates :content, {presence: true}
|
220
|
+
|
221
|
+
end
|
222
|
+
|
223
|
+
```
|
224
|
+
|
249
225
|
### 試したこと
|
250
226
|
|
251
227
|
・サーバーの再起動
|