質問編集履歴
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -78,12 +78,12 @@
|
|
78
78
|
|
79
79
|
if self.commentable.comments_count == 0
|
80
80
|
|
81
|
+
self.commentable.any_response = false
|
82
|
+
|
83
|
+
else
|
84
|
+
|
81
85
|
self.commentable.any_response = true
|
82
86
|
|
83
|
-
else
|
84
|
-
|
85
|
-
self.commentable.any_response = false
|
86
|
-
|
87
87
|
end
|
88
88
|
|
89
89
|
true
|
1
更新しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,17 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
+
> comment.rb
|
8
|
+
|
7
|
-
```
|
9
|
+
```
|
10
|
+
|
11
|
+
# == Schema Information
|
12
|
+
|
13
|
+
#
|
14
|
+
|
15
|
+
# Table name: comments
|
16
|
+
|
17
|
+
#
|
8
18
|
|
9
19
|
# id :integer not null, primary key
|
10
20
|
|
@@ -20,6 +30,18 @@
|
|
20
30
|
|
21
31
|
# updated_at :datetime not null
|
22
32
|
|
33
|
+
#
|
34
|
+
|
35
|
+
# Indexes
|
36
|
+
|
37
|
+
#
|
38
|
+
|
39
|
+
# index_comments_on_commentable_id_and_commentable_type (commentable_id,commentable_type)
|
40
|
+
|
41
|
+
# index_comments_on_user_id (user_id)
|
42
|
+
|
43
|
+
#
|
44
|
+
|
23
45
|
|
24
46
|
|
25
47
|
class Comment < ApplicationRecord
|
@@ -30,18 +52,38 @@
|
|
30
52
|
|
31
53
|
|
32
54
|
|
33
|
-
|
55
|
+
validates :text, presence: true
|
56
|
+
|
57
|
+
|
58
|
+
|
34
|
-
|
59
|
+
before_create :increment_counter
|
60
|
+
|
61
|
+
before_destroy :decrement_counter
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
# 問題の箇所
|
68
|
+
|
69
|
+
# before_save -> after_save
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
# after_save でもダメでした
|
74
|
+
|
75
|
+
|
76
|
+
|
35
|
-
|
77
|
+
after_save do
|
36
78
|
|
37
79
|
if self.commentable.comments_count == 0
|
38
80
|
|
81
|
+
self.commentable.any_response = true
|
82
|
+
|
83
|
+
else
|
84
|
+
|
39
85
|
self.commentable.any_response = false
|
40
86
|
|
41
|
-
else
|
42
|
-
|
43
|
-
self.commentable.any_response = true
|
44
|
-
|
45
87
|
end
|
46
88
|
|
47
89
|
true
|
@@ -52,68 +94,270 @@
|
|
52
94
|
|
53
95
|
|
54
96
|
|
55
|
-
|
56
|
-
|
57
|
-
e
|
97
|
+
private
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
# Instead of counter_cache
|
104
|
+
|
105
|
+
def increment_counter
|
106
|
+
|
107
|
+
self.commentable_type.constantize.increment_counter("comments_count", self.commentable_id)
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
# Instead of counter_cache
|
112
|
+
|
113
|
+
def decrement_counter
|
114
|
+
|
115
|
+
self.commentable_type.constantize.decrement_counter("comments_count", self.commentable_id)
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
58
120
|
|
59
121
|
end
|
60
122
|
|
61
|
-
|
123
|
+
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
> article.rb
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
# == Schema Information
|
134
|
+
|
135
|
+
#
|
136
|
+
|
137
|
+
# Table name: articles
|
138
|
+
|
139
|
+
#
|
140
|
+
|
141
|
+
# id :integer not null, primary key
|
142
|
+
|
143
|
+
# text :string default(""), not null
|
144
|
+
|
145
|
+
# any_response :boolean default(FALSE), not null
|
146
|
+
|
147
|
+
# comments_count :integer default(0)
|
148
|
+
|
149
|
+
# article_category_id :integer not null
|
150
|
+
|
151
|
+
# user_id :integer not null
|
152
|
+
|
153
|
+
# created_at :datetime not null
|
154
|
+
|
155
|
+
# updated_at :datetime not null
|
156
|
+
|
157
|
+
#
|
158
|
+
|
159
|
+
# Indexes
|
160
|
+
|
161
|
+
#
|
162
|
+
|
163
|
+
# index_articles_on_article_category_id (article_category_id)
|
164
|
+
|
165
|
+
# index_articles_on_user_id (user_id)
|
166
|
+
|
167
|
+
#
|
62
168
|
|
63
169
|
|
64
170
|
|
65
171
|
class Article < ApplicationRecord
|
66
172
|
|
173
|
+
validates :text,
|
174
|
+
|
175
|
+
presence: true
|
176
|
+
|
177
|
+
|
178
|
+
|
67
|
-
belongs_to :user,
|
179
|
+
belongs_to :user, counter_cache: true
|
68
|
-
|
180
|
+
|
69
|
-
counter_cache:
|
181
|
+
belongs_to :article_category, counter_cache: true
|
70
182
|
|
71
183
|
|
72
184
|
|
73
185
|
has_many :comments, as: :commentable
|
74
186
|
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
before_save :add_user_id
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def add_user_id
|
198
|
+
|
199
|
+
self.user_id = self.article_category.user_id
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
75
205
|
end
|
76
206
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
:te
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
:
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
:user
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
207
|
+
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
indexのページで、ajaxを使ってarticle#showを呼び出し
|
214
|
+
|
215
|
+
そこに、article.comments.each でコメント一覧と
|
216
|
+
|
217
|
+
article.comments.build でフォームを出してます。
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
= simple_form_for(article.comments.build, url: article_comments_path(article), remote: true) do |f|
|
224
|
+
|
225
|
+
.row
|
226
|
+
|
227
|
+
= f.input :text
|
228
|
+
|
229
|
+
.row
|
230
|
+
|
231
|
+
= f.button :submit
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
> comments_controller.rb
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
```
|
244
|
+
|
245
|
+
class Users::Public::CommentsController < AuthenticateUserController
|
246
|
+
|
247
|
+
before_action :load_commentable
|
248
|
+
|
249
|
+
respond_to :html, :js
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
def create
|
254
|
+
|
255
|
+
@comment = @commentable.comments.build(comment_params)
|
256
|
+
|
257
|
+
@comment.user_id = current_user.id
|
258
|
+
|
259
|
+
@comment.save
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
private
|
270
|
+
|
271
|
+
def load_commentable
|
272
|
+
|
273
|
+
resource, id = request.path.split('/')[1, 2]
|
274
|
+
|
275
|
+
@commentable = resource.singularize.classify.constantize.find(id)
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
def comment_params
|
282
|
+
|
283
|
+
params.require(:comment).permit(:text, :user_id)
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
```
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
```
|
298
|
+
|
299
|
+
$ > rails console
|
300
|
+
|
301
|
+
$ > @article = Article.first
|
302
|
+
|
303
|
+
#<Article:0x007f9a0a237cc8> {
|
304
|
+
|
305
|
+
:id => 1,
|
306
|
+
|
307
|
+
:text => "Nemo odit consectetur impedit.",
|
308
|
+
|
309
|
+
:any_response => false,
|
310
|
+
|
311
|
+
:comments_count => 0,
|
312
|
+
|
313
|
+
:thumbs_count => 0,
|
314
|
+
|
315
|
+
:article_category_id => 1,
|
316
|
+
|
317
|
+
:user_id => 1
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
$ > @article.comments.create(user_id: 1, text: 'hello')
|
324
|
+
|
325
|
+
#<Comment:0x007f9a0b0d4c90> {
|
326
|
+
|
327
|
+
:id => 1,
|
328
|
+
|
329
|
+
:text => "hello",
|
330
|
+
|
331
|
+
:user_id => 1,
|
332
|
+
|
333
|
+
:commentable_id => 1,
|
334
|
+
|
335
|
+
:commentable_type => "Article"
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
$ > @article.any_response
|
342
|
+
|
343
|
+
false
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
$ > Comment.last
|
348
|
+
|
349
|
+
#<Comment:0x007f9a0dd4cbb0> {
|
350
|
+
|
351
|
+
:id => 1,
|
352
|
+
|
353
|
+
:text => "hello",
|
354
|
+
|
355
|
+
:user_id => 1,
|
356
|
+
|
357
|
+
:commentable_id => 1,
|
358
|
+
|
359
|
+
:commentable_type => "Article"
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
```
|