質問編集履歴

6

f

2021/03/19 12:22

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -366,7 +366,7 @@
366
366
 
367
367
 
368
368
 
369
- ```
369
+ ```.js
370
370
 
371
371
  data() {
372
372
 
@@ -376,8 +376,6 @@
376
376
 
377
377
  text: "",
378
378
 
379
- tweet: [],
380
-
381
379
  errors: ''
382
380
 
383
381
  }

5

eeeee

2021/03/19 12:22

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -362,22 +362,80 @@
362
362
 
363
363
 
364
364
 
365
- ####エラー
365
+ ####解決
366
-
366
+
367
+
368
+
367
- ```
369
+ ```
370
+
368
-
371
+ data() {
372
+
373
+ return {
374
+
375
+ comment: "",
376
+
377
+ text: "",
378
+
379
+ tweet: [],
380
+
369
- vue.esm.js:648 [Vue warn]: Error in render: "TypeError: Cannot read property 'url' of undefined"
381
+ errors: ''
382
+
370
-
383
+ }
384
+
371
-
385
+ },
372
-
386
+
373
- found in
387
+ mounted() {
388
+
374
-
389
+ this.fetchComments()
390
+
375
-
391
+ },
392
+
376
-
393
+ methods: {
394
+
395
+
396
+
397
+ //コメント
398
+
399
+ fetchComments() {
400
+
401
+ axios
402
+
377
- ---> <TweetShowPage> at app/javascript/TweetShowPage.vue
403
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments`)
404
+
378
-
405
+ .then(response => {
406
+
407
+ this.comment = response.data
408
+
409
+ })
410
+
411
+ },
412
+
413
+ createComment: function() {
414
+
415
+ axios
416
+
417
+ .post(`/api/v1/tweets/${this.$route.params.id}/comments`,{text: this.text})
418
+
419
+ .then(response => {
420
+
379
- <App> at app/javascript/app.vue
421
+ // this.text = response.data;
422
+
380
-
423
+ this.fetchComments()
424
+
425
+ })
426
+
381
- <Root>
427
+ .catch(error => {
428
+
382
-
429
+ console.error(error);
430
+
431
+ if (error.response.data && error.response.data.errors) {
432
+
433
+ this.errors = error.response.data.errors;
434
+
435
+ }
436
+
437
+ });
438
+
439
+ }
440
+
383
- ```
441
+ ```

4

r

2021/03/19 12:17

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -222,7 +222,7 @@
222
222
 
223
223
 
224
224
 
225
- Vue
225
+ ####Vue
226
226
 
227
227
 
228
228
 
@@ -359,3 +359,25 @@
359
359
  </script>
360
360
 
361
361
  ```
362
+
363
+
364
+
365
+ ####エラー
366
+
367
+ ```
368
+
369
+ vue.esm.js:648 [Vue warn]: Error in render: "TypeError: Cannot read property 'url' of undefined"
370
+
371
+
372
+
373
+ found in
374
+
375
+
376
+
377
+ ---> <TweetShowPage> at app/javascript/TweetShowPage.vue
378
+
379
+ <App> at app/javascript/app.vue
380
+
381
+ <Root>
382
+
383
+ ```

3

g

2021/03/18 15:33

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -219,3 +219,143 @@
219
219
  end
220
220
 
221
221
  ```
222
+
223
+
224
+
225
+ Vue
226
+
227
+
228
+
229
+ ```
230
+
231
+ <div class="comment-content_tweet">
232
+
233
+ <div id="comments_area">
234
+
235
+ <div v-for="e in text" :key="e.id">
236
+
237
+ {{e.comment}}
238
+
239
+ </div>
240
+
241
+ </div>
242
+
243
+ <div class="comment-form">
244
+
245
+ <form @submit.prevent="createComment">
246
+
247
+ <div v-if="errors.length != 0">
248
+
249
+ <ul v-for="e in errors" :key="e">
250
+
251
+ <li><font color="red">{{ e }}</font></li>
252
+
253
+ </ul>
254
+
255
+ </div>
256
+
257
+ <div class="tweet-comment_form">
258
+
259
+ <textarea v-model="comment.text" type="text"></textarea>
260
+
261
+ </div>
262
+
263
+ <button type="submit" class="game_record" >投稿する</button>
264
+
265
+ </form>
266
+
267
+ </div>
268
+
269
+ </div>
270
+
271
+ </div>
272
+
273
+ </div>
274
+
275
+ </template>
276
+
277
+
278
+
279
+ <script>
280
+
281
+ <script>
282
+
283
+ import axios from 'axios'
284
+
285
+
286
+
287
+ data() {
288
+
289
+ return {
290
+
291
+ comment: {
292
+
293
+ text: ''
294
+
295
+ },
296
+
297
+ errors: ''
298
+
299
+ }
300
+
301
+ },
302
+
303
+ mounted() {
304
+
305
+ this.fetchTweets()
306
+
307
+ this.fetchComments()
308
+
309
+ },
310
+
311
+ methods: {
312
+
313
+ fetchComments() {
314
+
315
+ axios
316
+
317
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
318
+
319
+ .then(response => {
320
+
321
+ this.text = response.data
322
+
323
+ })
324
+
325
+ },
326
+
327
+ createComment: function() {
328
+
329
+ axios
330
+
331
+ .post(`/api/v1/tweets/${this.$route.params.id}/comments`,this.comment)
332
+
333
+ .then(response => {
334
+
335
+ let e = response.data;
336
+
337
+ this.fetchComments()
338
+
339
+ })
340
+
341
+ .catch(error => {
342
+
343
+ console.error(error);
344
+
345
+ if (error.response.data && error.response.data.errors) {
346
+
347
+ this.errors = error.response.data.errors;
348
+
349
+ }
350
+
351
+ });
352
+
353
+ }
354
+
355
+ }
356
+
357
+ }
358
+
359
+ </script>
360
+
361
+ ```

2

r

2021/03/18 15:31

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -155,3 +155,67 @@
155
155
  </script>
156
156
 
157
157
  ```
158
+
159
+
160
+
161
+
162
+
163
+ ###追記
164
+
165
+ Railsと組み合わせてコメント機能を作っています
166
+
167
+ コントローラーでデータベースからjson形式でデータを取得しています
168
+
169
+
170
+
171
+ ```
172
+
173
+ class Api::V1::CommentsController < ApiController
174
+
175
+
176
+
177
+ # ActiveRecordのレコードが見つからなければ404 not foundを応答する
178
+
179
+ rescue_from ActiveRecord::RecordNotFound, with: :render_status_404
180
+
181
+
182
+
183
+ def index
184
+
185
+ @comments = Comment.where(tweet_id: params[:tweet_id])
186
+
187
+ render 'index', formats: 'json', handlers: 'jbuilder'
188
+
189
+ end
190
+
191
+
192
+
193
+ def create
194
+
195
+ comment = Comment.new(comment_params)
196
+
197
+ if comment.save
198
+
199
+ render json: comment,status: :created
200
+
201
+ else
202
+
203
+ render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
204
+
205
+ end
206
+
207
+ end
208
+
209
+
210
+
211
+ private
212
+
213
+ def comment_params
214
+
215
+ params.require(:comment).permit(:text).merge(user_id: current_user.id,tweet_id: params[:tweet_id])
216
+
217
+ end
218
+
219
+ end
220
+
221
+ ```

1

っっっっr

2021/03/18 11:49

投稿

Meitoku
Meitoku

スコア44

test CHANGED
@@ -1 +1 @@
1
- Vue.js モーダルウィンドウ
1
+ Vue.js 投稿を非同期に
test CHANGED
@@ -2,21 +2,13 @@
2
2
 
3
3
  ![イメージ説明](25dd745cf844b8ef97f2d73ac00d48d4.png)
4
4
 
5
- Vue.jsでモーダルウィンドウで投稿フォームを表示してコメントを投稿できる仕組みにしようと思っています
5
+ Vue.jsでモーダルウィンドウで投稿フォームを表示してコメントを投稿できる仕組みにしています
6
6
 
7
- 投稿フォームまではできのですがッセージの表示をどのような実装にすれば良のか分かりせん
7
+ 投稿はできのですがリロードしないとコントができないようってしまいま
8
8
 
9
- チャットのように画面を更新させずにメッセージを表示させたいと思っています
9
+ のようにすれば非同期で表示できるのでしょうか?
10
10
 
11
11
 
12
-
13
- 投稿フォームを実装したファイルと同じファイルにコメントを表示させるのか、別々に実装させるのかアドバイスを頂きたいです
14
-
15
-
16
-
17
-
18
-
19
- ###comment.vue(フォーム実装済み)
20
12
 
21
13
 
22
14
 
@@ -25,6 +17,12 @@
25
17
  <template>
26
18
 
27
19
  <div class="tweet-comment">
20
+
21
+ <div v-for="e in text" :key="e.id">
22
+
23
+ {{e.comment}}
24
+
25
+ </div>
28
26
 
29
27
  <modal name="comment-modal">
30
28
 
@@ -84,6 +82,12 @@
84
82
 
85
83
  },
86
84
 
85
+ mounted() {
86
+
87
+ this.fetchComments()
88
+
89
+ },
90
+
87
91
  methods: {
88
92
 
89
93
  createComment: function() {
@@ -112,6 +116,20 @@
112
116
 
113
117
  },
114
118
 
119
+ fetchComments() {
120
+
121
+ axios
122
+
123
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
124
+
125
+ .then(response => {
126
+
127
+ this.text = response.data
128
+
129
+ })
130
+
131
+ },
132
+
115
133
  showModal(){
116
134
 
117
135
  this.$modal.show('comment-modal');
@@ -137,65 +155,3 @@
137
155
  </script>
138
156
 
139
157
  ```
140
-
141
-
142
-
143
- コメント表示は恐らく以下のようなコードになると思います
144
-
145
- こういう処理を同じファイルで実装するのでしょうか?
146
-
147
-
148
-
149
- ```
150
-
151
- <div v-for="e in comments" :key="e.id">
152
-
153
- {{e.comment}}
154
-
155
- </div>
156
-
157
-
158
-
159
- <script>
160
-
161
- import axios from 'axios'
162
-
163
-
164
-
165
- export default {
166
-
167
- data() {
168
-
169
- comments: []
170
-
171
- },
172
-
173
- mounted() {
174
-
175
- this.fetchComments()
176
-
177
- },
178
-
179
- methods: {
180
-
181
- fetchComments() {
182
-
183
- axios
184
-
185
- .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
186
-
187
- .then(response => {
188
-
189
- this.comments = response.data
190
-
191
- })
192
-
193
- }
194
-
195
- }
196
-
197
- }
198
-
199
- </script>
200
-
201
- ```