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

質問編集履歴

6

f

2021/03/19 12:22

投稿

Meitoku
Meitoku

スコア44

title CHANGED
File without changes
body CHANGED
@@ -182,12 +182,11 @@
182
182
 
183
183
  ####解決
184
184
 
185
- ```
185
+ ```.js
186
186
  data() {
187
187
  return {
188
188
  comment: "",
189
189
  text: "",
190
- tweet: [],
191
190
  errors: ''
192
191
  }
193
192
  },

5

eeeee

2021/03/19 12:22

投稿

Meitoku
Meitoku

スコア44

title CHANGED
File without changes
body CHANGED
@@ -180,13 +180,42 @@
180
180
  </script>
181
181
  ```
182
182
 
183
- ####エラー
183
+ ####解決
184
+
184
185
  ```
186
+ data() {
187
+ return {
188
+ comment: "",
189
+ text: "",
190
+ tweet: [],
185
- vue.esm.js:648 [Vue warn]: Error in render: "TypeError: Cannot read property 'url' of undefined"
191
+ errors: ''
192
+ }
193
+ },
194
+ mounted() {
195
+ this.fetchComments()
196
+ },
197
+ methods: {
186
198
 
199
+ //コメント
187
- found in
200
+ fetchComments() {
188
-
201
+ axios
189
- ---> <TweetShowPage> at app/javascript/TweetShowPage.vue
202
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments`)
203
+ .then(response => {
204
+ this.comment = response.data
205
+ })
206
+ },
207
+ createComment: function() {
208
+ axios
209
+ .post(`/api/v1/tweets/${this.$route.params.id}/comments`,{text: this.text})
210
+ .then(response => {
190
- <App> at app/javascript/app.vue
211
+ // this.text = response.data;
212
+ this.fetchComments()
213
+ })
191
- <Root>
214
+ .catch(error => {
215
+ console.error(error);
216
+ if (error.response.data && error.response.data.errors) {
217
+ this.errors = error.response.data.errors;
218
+ }
219
+ });
220
+ }
192
221
  ```

4

r

2021/03/19 12:17

投稿

Meitoku
Meitoku

スコア44

title CHANGED
File without changes
body CHANGED
@@ -110,7 +110,7 @@
110
110
  end
111
111
  ```
112
112
 
113
- Vue
113
+ ####Vue
114
114
 
115
115
  ```
116
116
  <div class="comment-content_tweet">
@@ -178,4 +178,15 @@
178
178
  }
179
179
  }
180
180
  </script>
181
+ ```
182
+
183
+ ####エラー
184
+ ```
185
+ vue.esm.js:648 [Vue warn]: Error in render: "TypeError: Cannot read property 'url' of undefined"
186
+
187
+ found in
188
+
189
+ ---> <TweetShowPage> at app/javascript/TweetShowPage.vue
190
+ <App> at app/javascript/app.vue
191
+ <Root>
181
192
  ```

3

g

2021/03/18 15:33

投稿

Meitoku
Meitoku

スコア44

title CHANGED
File without changes
body CHANGED
@@ -108,4 +108,74 @@
108
108
  params.require(:comment).permit(:text).merge(user_id: current_user.id,tweet_id: params[:tweet_id])
109
109
  end
110
110
  end
111
+ ```
112
+
113
+ Vue
114
+
115
+ ```
116
+ <div class="comment-content_tweet">
117
+ <div id="comments_area">
118
+ <div v-for="e in text" :key="e.id">
119
+ {{e.comment}}
120
+ </div>
121
+ </div>
122
+ <div class="comment-form">
123
+ <form @submit.prevent="createComment">
124
+ <div v-if="errors.length != 0">
125
+ <ul v-for="e in errors" :key="e">
126
+ <li><font color="red">{{ e }}</font></li>
127
+ </ul>
128
+ </div>
129
+ <div class="tweet-comment_form">
130
+ <textarea v-model="comment.text" type="text"></textarea>
131
+ </div>
132
+ <button type="submit" class="game_record" >投稿する</button>
133
+ </form>
134
+ </div>
135
+ </div>
136
+ </div>
137
+ </div>
138
+ </template>
139
+
140
+ <script>
141
+ <script>
142
+ import axios from 'axios'
143
+
144
+ data() {
145
+ return {
146
+ comment: {
147
+ text: ''
148
+ },
149
+ errors: ''
150
+ }
151
+ },
152
+ mounted() {
153
+ this.fetchTweets()
154
+ this.fetchComments()
155
+ },
156
+ methods: {
157
+ fetchComments() {
158
+ axios
159
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
160
+ .then(response => {
161
+ this.text = response.data
162
+ })
163
+ },
164
+ createComment: function() {
165
+ axios
166
+ .post(`/api/v1/tweets/${this.$route.params.id}/comments`,this.comment)
167
+ .then(response => {
168
+ let e = response.data;
169
+ this.fetchComments()
170
+ })
171
+ .catch(error => {
172
+ console.error(error);
173
+ if (error.response.data && error.response.data.errors) {
174
+ this.errors = error.response.data.errors;
175
+ }
176
+ });
177
+ }
178
+ }
179
+ }
180
+ </script>
111
181
  ```

2

r

2021/03/18 15:31

投稿

Meitoku
Meitoku

スコア44

title CHANGED
File without changes
body CHANGED
@@ -76,4 +76,36 @@
76
76
  }
77
77
  }
78
78
  </script>
79
+ ```
80
+
81
+
82
+ ###追記
83
+ Railsと組み合わせてコメント機能を作っています
84
+ コントローラーでデータベースからjson形式でデータを取得しています
85
+
86
+ ```
87
+ class Api::V1::CommentsController < ApiController
88
+
89
+ # ActiveRecordのレコードが見つからなければ404 not foundを応答する
90
+ rescue_from ActiveRecord::RecordNotFound, with: :render_status_404
91
+
92
+ def index
93
+ @comments = Comment.where(tweet_id: params[:tweet_id])
94
+ render 'index', formats: 'json', handlers: 'jbuilder'
95
+ end
96
+
97
+ def create
98
+ comment = Comment.new(comment_params)
99
+ if comment.save
100
+ render json: comment,status: :created
101
+ else
102
+ render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
103
+ end
104
+ end
105
+
106
+ private
107
+ def comment_params
108
+ params.require(:comment).permit(:text).merge(user_id: current_user.id,tweet_id: params[:tweet_id])
109
+ end
110
+ end
79
111
  ```

1

っっっっr

2021/03/18 11:49

投稿

Meitoku
Meitoku

スコア44

title CHANGED
@@ -1,1 +1,1 @@
1
- Vue.js モーダルウィンドウ
1
+ Vue.js 投稿を非同期に
body CHANGED
@@ -1,17 +1,16 @@
1
1
  フォームイメージ
2
2
  ![イメージ説明](25dd745cf844b8ef97f2d73ac00d48d4.png)
3
- Vue.jsでモーダルウィンドウで投稿フォームを表示してコメントを投稿できる仕組みにしようと思っています
3
+ Vue.jsでモーダルウィンドウで投稿フォームを表示してコメントを投稿できる仕組みにしています
4
- 投稿フォームまではできのですが、メッセジの表示をどのような実装すれば良のか分かりせん
4
+ 投稿はできのですがリロドしないとコメントができないようになってしまいま
5
- チャットのように画面を更新させずにメッセージを表示させたいと思っています
5
+ のようにすれば非同期で表示できるのでしょうか?
6
6
 
7
- 投稿フォームを実装したファイルと同じファイルにコメントを表示させるのか、別々に実装させるのかアドバイスを頂きたいです
8
7
 
9
-
10
- ###comment.vue(フォーム実装済み)
11
-
12
8
  ```
13
9
  <template>
14
10
  <div class="tweet-comment">
11
+ <div v-for="e in text" :key="e.id">
12
+ {{e.comment}}
13
+ </div>
15
14
  <modal name="comment-modal">
16
15
  <form @submit.prevent="createComment">
17
16
  <div v-if="errors.length != 0">
@@ -41,6 +40,9 @@
41
40
  errors: ''
42
41
  }
43
42
  },
43
+ mounted() {
44
+ this.fetchComments()
45
+ },
44
46
  methods: {
45
47
  createComment: function() {
46
48
  axios
@@ -55,6 +57,13 @@
55
57
  }
56
58
  });
57
59
  },
60
+ fetchComments() {
61
+ axios
62
+ .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
63
+ .then(response => {
64
+ this.text = response.data
65
+ })
66
+ },
58
67
  showModal(){
59
68
  this.$modal.show('comment-modal');
60
69
  },
@@ -67,35 +76,4 @@
67
76
  }
68
77
  }
69
78
  </script>
70
- ```
71
-
72
- コメント表示は恐らく以下のようなコードになると思います
73
- こういう処理を同じファイルで実装するのでしょうか?
74
-
75
- ```
76
- <div v-for="e in comments" :key="e.id">
77
- {{e.comment}}
78
- </div>
79
-
80
- <script>
81
- import axios from 'axios'
82
-
83
- export default {
84
- data() {
85
- comments: []
86
- },
87
- mounted() {
88
- this.fetchComments()
89
- },
90
- methods: {
91
- fetchComments() {
92
- axios
93
- .get(`/api/v1/tweets/${this.$route.params.id}/comments.json`)
94
- .then(response => {
95
- this.comments = response.data
96
- })
97
- }
98
- }
99
- }
100
- </script>
101
79
  ```