質問編集履歴
6
一覧の部分のコードを一度更新しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,14 +30,324 @@
|
|
30
30
|
|
31
31
|
Like.create(user_id: current_user.id, post_id: params[:id])
|
32
32
|
|
33
|
-
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
34
36
|
|
35
37
|
def destroy
|
36
38
|
|
37
39
|
Like.find_by(user_id: current_user.id, post_id: params[:id]).destroy
|
38
40
|
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def post_params
|
50
|
+
|
51
|
+
@post = Post.find(params[:id])
|
52
|
+
|
53
|
+
end
|
54
|
+
|
39
55
|
end
|
40
56
|
|
57
|
+
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
#問題のソースコード
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
ルーティング
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
Rails.application.routes.draw do
|
72
|
+
|
73
|
+
devise_for :users
|
74
|
+
|
75
|
+
resources :movies do
|
76
|
+
|
77
|
+
resources :movie_comments, only: %i[create destroy]
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
resources :posts do
|
82
|
+
|
83
|
+
resources :comments, only: %i[create destroy]
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
post 'like/:id', to: 'likes#create', as: 'create_like'
|
88
|
+
|
89
|
+
delete 'like/:id', to: 'likes#destroy', as: 'destroy_like'
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
post 'movie_like/:id', to: 'movie_likes#create', as: 'create_movie_like'
|
94
|
+
|
95
|
+
delete 'movie_like/:id', to: 'movie_likes#destroy', as: 'destroy_movie_like'
|
96
|
+
|
97
|
+
root 'posts#index'
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
いいね機能に関する部分テンプレート(_like.html.erb)
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
<% if user_signed_in? %>
|
110
|
+
|
111
|
+
<div class="like">
|
112
|
+
|
113
|
+
<h3>いいね件数: <%= post.likes.count %></h3>
|
114
|
+
|
115
|
+
<div class = 'like-button'>
|
116
|
+
|
117
|
+
<% if current_user.liked_by?(post.id) %>
|
118
|
+
|
119
|
+
<td><%= link_to 'いいねを外す', destroy_like_path(post), class: "like-link", method: :DELETE,remote: true %></td>
|
120
|
+
|
121
|
+
<i class="fa fa-heart unlike-btn"></i>
|
122
|
+
|
123
|
+
<% else %>
|
124
|
+
|
125
|
+
<td><%= link_to 'いいね', create_like_path(post), class: "like-link", method: :post, remote: true %></td>
|
126
|
+
|
127
|
+
<i class="fa fa-heart like-btn"></i>
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
<% end %>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
</div>
|
136
|
+
|
137
|
+
</div>
|
138
|
+
|
139
|
+
<% end %>
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
いいね機能(ajax)部分に関するテンプレート
|
146
|
+
|
147
|
+
名前:create.js.erb
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
document.getElementById('post_<%= @post.id %>').innerHTML = '<%= j(render @post) %>'
|
152
|
+
|
153
|
+
alert('いいねが押せている!');
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
同じくいいね機能(ajax)部分に関するテンプレート
|
160
|
+
|
161
|
+
名前:destroy.js.erb
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
document.getElementById('post_<%= @post.id %>').innerHTML = '<%= j(render @post) %>'
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
alertは確認のために載せているので、後々削除します
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
投稿一覧のコード
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
<div class="content-wrapper">
|
182
|
+
|
183
|
+
<div class="content-block">
|
184
|
+
|
185
|
+
<% @posts.each do |post| %>
|
186
|
+
|
187
|
+
<div class="content">
|
188
|
+
|
189
|
+
<div class="user-about">
|
190
|
+
|
191
|
+
<div class="image">
|
192
|
+
|
193
|
+
<% if post.user.image.attached? %>
|
194
|
+
|
195
|
+
<%= image_tag post.user.image %>
|
196
|
+
|
197
|
+
<% else %>
|
198
|
+
|
199
|
+
<%= image_tag 'no.user.png' %>
|
200
|
+
|
201
|
+
<% end %>
|
202
|
+
|
203
|
+
</div>
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
<div class="profile">
|
208
|
+
|
209
|
+
<div class="name-history">
|
210
|
+
|
211
|
+
<div class="name">
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<%= post.user.nickname %>
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
</div>
|
222
|
+
|
223
|
+
<div class="mania-histry">
|
224
|
+
|
225
|
+
<%= "学習歴:#{post.user.mania_histry}年" %>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
</div>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
<div class="enjoy-point">
|
234
|
+
|
235
|
+
<%= "楽しいポイント#{post.user.enjoy_point}"%>
|
236
|
+
|
237
|
+
</div>
|
238
|
+
|
239
|
+
</div>
|
240
|
+
|
241
|
+
</div>
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
<div class="text">
|
246
|
+
|
247
|
+
<p><%= post.content %></p>
|
248
|
+
|
249
|
+
</div>
|
250
|
+
|
251
|
+
<% if post.images.attached? %>
|
252
|
+
|
253
|
+
<% post.images.each do |image| %>
|
254
|
+
|
255
|
+
<div class = 'images'>
|
256
|
+
|
257
|
+
<%= image_tag image, class: "content-image" %>
|
258
|
+
|
259
|
+
</div>
|
260
|
+
|
261
|
+
<% end %>
|
262
|
+
|
263
|
+
<% end %>
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
<div class="action-menu">
|
268
|
+
|
269
|
+
<tr id="post_<%= post.id %>">
|
270
|
+
|
271
|
+
<%= render 'post', post: post %>
|
272
|
+
|
273
|
+
</tr>
|
274
|
+
|
275
|
+
<div class="comment">
|
276
|
+
|
277
|
+
<h3>コメント件数: <%= post.comments.count %></h3>
|
278
|
+
|
279
|
+
<%= link_to "コメントする", "/posts/#{post.id}", class: "comment-buttom" %>
|
280
|
+
|
281
|
+
</div>
|
282
|
+
|
283
|
+
<%if user_signed_in?%>
|
284
|
+
|
285
|
+
<% if current_user.id == post.user.id %>
|
286
|
+
|
287
|
+
<%= link_to "編集", edit_post_path(post) %>
|
288
|
+
|
289
|
+
<%= link_to "削除", post_path(post), method: :delete %>
|
290
|
+
|
291
|
+
<% end %>
|
292
|
+
|
293
|
+
<% end %>
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
</div>
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
</div>
|
302
|
+
|
303
|
+
<% end %>
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
</div>
|
310
|
+
|
311
|
+
<div class="sidebar">
|
312
|
+
|
313
|
+
<%= render 'shared/menu'%>
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
</div>
|
318
|
+
|
319
|
+
</div>
|
320
|
+
|
321
|
+
```
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
##続いていいね機能に関するコントローラーです
|
326
|
+
|
327
|
+
```
|
328
|
+
|
329
|
+
class LikesController < ApplicationController
|
330
|
+
|
331
|
+
before_action :post_params
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
def create
|
336
|
+
|
337
|
+
Like.create(user_id: current_user.id, post_id: params[:id])
|
338
|
+
|
339
|
+
end
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
def destroy
|
344
|
+
|
345
|
+
Like.find_by(user_id: current_user.id, post_id: params[:id]).destroy
|
346
|
+
|
347
|
+
end
|
348
|
+
|
349
|
+
|
350
|
+
|
41
351
|
private
|
42
352
|
|
43
353
|
|
@@ -50,45 +360,23 @@
|
|
50
360
|
|
51
361
|
end
|
52
362
|
|
363
|
+
|
364
|
+
|
53
|
-
```
|
365
|
+
```
|
54
|
-
|
55
|
-
|
56
|
-
|
366
|
+
|
367
|
+
|
368
|
+
|
57
|
-
#
|
369
|
+
##続いていいね機能に関するモデルです
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
370
|
+
|
62
|
-
|
371
|
+
|
372
|
+
|
63
|
-
```
|
373
|
+
```
|
64
|
-
|
374
|
+
|
65
|
-
|
375
|
+
class Like < ApplicationRecord
|
66
|
-
|
376
|
+
|
67
|
-
|
377
|
+
belongs_to :user
|
68
|
-
|
69
|
-
|
378
|
+
|
70
|
-
|
71
|
-
resources :movie_comments, only: %i[create destroy]
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
|
379
|
+
belongs_to :post
|
76
|
-
|
77
|
-
resources :comments, only: %i[create destroy]
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
post 'like/:id', to: 'likes#create', as: 'create_like'
|
82
|
-
|
83
|
-
delete 'like/:id', to: 'likes#destroy', as: 'destroy_like'
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
post 'movie_like/:id', to: 'movie_likes#create', as: 'create_movie_like'
|
88
|
-
|
89
|
-
delete 'movie_like/:id', to: 'movie_likes#destroy', as: 'destroy_movie_like'
|
90
|
-
|
91
|
-
root 'posts#index'
|
92
380
|
|
93
381
|
end
|
94
382
|
|
@@ -96,265 +384,97 @@
|
|
96
384
|
|
97
385
|
```
|
98
386
|
|
99
|
-
いいね
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
</div>
|
132
|
-
|
133
|
-
<% end %>
|
134
|
-
|
135
|
-
```
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
いいね機能(ajax)部分に関するテンプレート
|
140
|
-
|
141
|
-
名前:create.js.erb
|
142
|
-
|
143
|
-
```
|
144
|
-
|
145
|
-
document.getElementById('like_<%= @post.id %>').innerHTML = '<%= j(render @post) %>'
|
146
|
-
|
147
|
-
alert('いいねが押せている!');
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
```
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
同じくいいね機能(ajax)部分に関するテンプレート
|
156
|
-
|
157
|
-
名前:destroy.js.erb
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
```
|
162
|
-
|
163
|
-
document.getElementById('post_<%= @post.id %>').innerHTML = '<%= j(render @post) %>'
|
164
|
-
|
165
|
-
alert('いいねが解除できている!');
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
```
|
170
|
-
|
171
|
-
alertは確認のために載せているので、後々削除します
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
投稿一覧のコード
|
176
|
-
|
177
|
-
```
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
<div class="content-wrapper">
|
182
|
-
|
183
|
-
<div class="content-block">
|
184
|
-
|
185
|
-
<% @posts.each do |post| %>
|
186
|
-
|
187
|
-
<div class="content">
|
188
|
-
|
189
|
-
<div class="user-about">
|
190
|
-
|
191
|
-
<div class="image">
|
192
|
-
|
193
|
-
<% if post.user.image.attached? %>
|
194
|
-
|
195
|
-
<%= image_tag post.user.image %>
|
196
|
-
|
197
|
-
<% else %>
|
198
|
-
|
199
|
-
<%= image_tag 'no.user.png' %>
|
200
|
-
|
201
|
-
<% end %>
|
202
|
-
|
203
|
-
</div>
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
<div class="profile">
|
208
|
-
|
209
|
-
<div class="name-history">
|
210
|
-
|
211
|
-
<div class="name">
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
<%= post.user.nickname %>
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
</div>
|
222
|
-
|
223
|
-
<div class="mania-histry">
|
224
|
-
|
225
|
-
<%= "学習歴:#{post.user.mania_histry}年" %>
|
226
|
-
|
227
|
-
</div>
|
228
|
-
|
229
|
-
</div>
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
<div class="enjoy-point">
|
234
|
-
|
235
|
-
<%= "楽しいポイント#{post.user.enjoy_point}"%>
|
236
|
-
|
237
|
-
</div>
|
238
|
-
|
239
|
-
</div>
|
240
|
-
|
241
|
-
</div>
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
<div class="text">
|
246
|
-
|
247
|
-
<p><%= post.content %></p>
|
248
|
-
|
249
|
-
</div>
|
250
|
-
|
251
|
-
<% if post.images.attached? %>
|
252
|
-
|
253
|
-
<% post.images.each do |image| %>
|
254
|
-
|
255
|
-
<div class = 'images'>
|
256
|
-
|
257
|
-
<%= image_tag image, class: "content-image" %>
|
258
|
-
|
259
|
-
</div>
|
260
|
-
|
261
|
-
<% end %>
|
262
|
-
|
263
|
-
<% end %>
|
264
|
-
|
265
|
-
<div class="action-menu">
|
266
|
-
|
267
|
-
<tr id="post_<%= post.id %>">
|
268
|
-
|
269
|
-
<%= render 'post', post: post %>
|
270
|
-
|
271
|
-
</tr>
|
272
|
-
|
273
|
-
<div class="comment">
|
274
|
-
|
275
|
-
<h3>コメント件数: <%= post.comments.count %></h3>
|
276
|
-
|
277
|
-
<%= link_to "コメントする", "/posts/#{post.id}", class: "comment-buttom" %>
|
278
|
-
|
279
|
-
</div>
|
280
|
-
|
281
|
-
<%if user_signed_in?%>
|
282
|
-
|
283
|
-
<% if current_user.id == post.user.id %>
|
284
|
-
|
285
|
-
<%= link_to "編集", edit_post_path(post) %>
|
286
|
-
|
287
|
-
<%= link_to "削除", post_path(post), method: :delete %>
|
288
|
-
|
289
|
-
<% end %>
|
290
|
-
|
291
|
-
<% end %>
|
387
|
+
いいねするユーザーに関するモデルです
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
```
|
392
|
+
|
393
|
+
class User < ApplicationRecord
|
394
|
+
|
395
|
+
# Include default devise modules. Others available are:
|
396
|
+
|
397
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
398
|
+
|
399
|
+
devise :database_authenticatable, :registerable,
|
400
|
+
|
401
|
+
:recoverable, :rememberable, :validatable
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
has_one_attached :image
|
406
|
+
|
407
|
+
has_many :posts
|
408
|
+
|
409
|
+
has_many :likes
|
410
|
+
|
411
|
+
has_many :movielikes
|
412
|
+
|
413
|
+
has_many :comments, dependent: :destroy
|
414
|
+
|
415
|
+
has_many :movies
|
416
|
+
|
417
|
+
|
292
418
|
|
293
419
|
|
294
420
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
e
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
def post_params
|
354
|
-
|
355
|
-
@post = Post.find(params[:id])
|
356
|
-
|
357
|
-
end
|
421
|
+
def liked_by?(post_id)
|
422
|
+
|
423
|
+
likes.where(post_id: post_id).exists?
|
424
|
+
|
425
|
+
end
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
with_options presence: true do
|
434
|
+
|
435
|
+
validates :nickname
|
436
|
+
|
437
|
+
validates :mania_histry
|
438
|
+
|
439
|
+
validates :enjoy_point
|
440
|
+
|
441
|
+
validates :email
|
442
|
+
|
443
|
+
validates :password, length: { minimum: 6 }
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
end
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
validate :image_presence
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
def image_presence
|
456
|
+
|
457
|
+
if image.attached?
|
458
|
+
|
459
|
+
if !image.content_type.in?(%('image/jpeg image/png'))
|
460
|
+
|
461
|
+
errors.add(:image, 'にはjpegまたはpngファイルを添付してください')
|
462
|
+
|
463
|
+
end
|
464
|
+
|
465
|
+
else
|
466
|
+
|
467
|
+
errors.add(:image, 'ファイルを添付してください')
|
468
|
+
|
469
|
+
end
|
470
|
+
|
471
|
+
end
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
|
358
478
|
|
359
479
|
end
|
360
480
|
|
@@ -362,122 +482,6 @@
|
|
362
482
|
|
363
483
|
|
364
484
|
|
365
|
-
##続いていいね機能に関するモデルです
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
```
|
370
|
-
|
371
|
-
class Like < ApplicationRecord
|
372
|
-
|
373
|
-
belongs_to :user
|
374
|
-
|
375
|
-
belongs_to :post
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
```
|
382
|
-
|
383
|
-
いいねするユーザーに関するモデルです
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
```
|
388
|
-
|
389
|
-
class User < ApplicationRecord
|
390
|
-
|
391
|
-
# Include default devise modules. Others available are:
|
392
|
-
|
393
|
-
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
394
|
-
|
395
|
-
devise :database_authenticatable, :registerable,
|
396
|
-
|
397
|
-
:recoverable, :rememberable, :validatable
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
has_one_attached :image
|
402
|
-
|
403
|
-
has_many :posts
|
404
|
-
|
405
|
-
has_many :likes
|
406
|
-
|
407
|
-
has_many :movielikes
|
408
|
-
|
409
|
-
has_many :comments, dependent: :destroy
|
410
|
-
|
411
|
-
has_many :movies
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
def liked_by?(post_id)
|
418
|
-
|
419
|
-
likes.where(post_id: post_id).exists?
|
420
|
-
|
421
|
-
end
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
with_options presence: true do
|
430
|
-
|
431
|
-
validates :nickname
|
432
|
-
|
433
|
-
validates :mania_histry
|
434
|
-
|
435
|
-
validates :enjoy_point
|
436
|
-
|
437
|
-
validates :email
|
438
|
-
|
439
|
-
validates :password, length: { minimum: 6 }
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
end
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
validate :image_presence
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
def image_presence
|
452
|
-
|
453
|
-
if image.attached?
|
454
|
-
|
455
|
-
if !image.content_type.in?(%('image/jpeg image/png'))
|
456
|
-
|
457
|
-
errors.add(:image, 'にはjpegまたはpngファイルを添付してください')
|
458
|
-
|
459
|
-
end
|
460
|
-
|
461
|
-
else
|
462
|
-
|
463
|
-
errors.add(:image, 'ファイルを添付してください')
|
464
|
-
|
465
|
-
end
|
466
|
-
|
467
|
-
end
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
end
|
476
|
-
|
477
|
-
```
|
478
|
-
|
479
|
-
|
480
|
-
|
481
485
|
#エラーログ
|
482
486
|
|
483
487
|
|
@@ -541,3 +545,11 @@
|
|
541
545
|
|
542
546
|
|
543
547
|
```
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
#ソースコードを表示をした結果
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
![イメージ説明](c1e7becda1ad0c5f9ef6015ddd05764b.png)
|
5
ルーティングの記述をしました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
jsで非同期通信を使ったいいね機能を実装したいのですが、
|
1
|
+
jsで非同期通信を使ったいいね機能を実装したいのですが、Processing by ActiveStorage::DiskController#show as JPEG
|
test
CHANGED
@@ -56,6 +56,46 @@
|
|
56
56
|
|
57
57
|
#問題のソースコード
|
58
58
|
|
59
|
+
|
60
|
+
|
61
|
+
ルーティング
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
Rails.application.routes.draw do
|
66
|
+
|
67
|
+
devise_for :users
|
68
|
+
|
69
|
+
resources :movies do
|
70
|
+
|
71
|
+
resources :movie_comments, only: %i[create destroy]
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
resources :posts do
|
76
|
+
|
77
|
+
resources :comments, only: %i[create destroy]
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
post 'like/:id', to: 'likes#create', as: 'create_like'
|
82
|
+
|
83
|
+
delete 'like/:id', to: 'likes#destroy', as: 'destroy_like'
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
post 'movie_like/:id', to: 'movie_likes#create', as: 'create_movie_like'
|
88
|
+
|
89
|
+
delete 'movie_like/:id', to: 'movie_likes#destroy', as: 'destroy_movie_like'
|
90
|
+
|
91
|
+
root 'posts#index'
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
```
|
98
|
+
|
59
99
|
いいね機能に関する部分テンプレート(_like.html.erb)
|
60
100
|
|
61
101
|
```
|
4
method:postに変更したときのエラーログを貼り付けました
test
CHANGED
File without changes
|
test
CHANGED
@@ -442,76 +442,62 @@
|
|
442
442
|
|
443
443
|
|
444
444
|
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
web_1 |
|
456
|
-
|
457
|
-
web_1 |
|
458
|
-
|
459
|
-
web_1 |
|
460
|
-
|
461
|
-
web_1 |
|
462
|
-
|
463
|
-
web_1 | /
|
464
|
-
|
465
|
-
web_1 |
|
466
|
-
|
467
|
-
web_1 | /
|
468
|
-
|
469
|
-
web_1 |
|
470
|
-
|
471
|
-
web_1 | /
|
472
|
-
|
473
|
-
web_1 |
|
474
|
-
|
475
|
-
web_1 | /
|
476
|
-
|
477
|
-
web_1 |
|
478
|
-
|
479
|
-
web_1 | /
|
480
|
-
|
481
|
-
web_1 |
|
482
|
-
|
483
|
-
web_1 | /
|
484
|
-
|
485
|
-
web_1 |
|
486
|
-
|
487
|
-
web_1 |
|
488
|
-
|
489
|
-
web_1 |
|
490
|
-
|
491
|
-
web_1 |
|
492
|
-
|
493
|
-
web_1 | /
|
494
|
-
|
495
|
-
web_1 |
|
496
|
-
|
497
|
-
web_1 |
|
498
|
-
|
499
|
-
web_1 |
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/configuration.rb:227:in `call'
|
506
|
-
|
507
|
-
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:706:in `handle_request'
|
508
|
-
|
509
|
-
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:476:in `process_client'
|
510
|
-
|
511
|
-
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:334:in `block in run'
|
512
|
-
|
513
|
-
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/thread_pool.rb:135:in `block in spawn_thread'
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
```
|
445
|
+
|
446
|
+
|
447
|
+
method: postに変更しました
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
```
|
452
|
+
|
453
|
+
Started DELETE "/like/2" for 172.24.0.1 at 2021-04-06 01:31:40 +0000
|
454
|
+
|
455
|
+
web_1 | Cannot render console from 172.24.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
|
456
|
+
|
457
|
+
web_1 | Processing by LikesController#destroy as JS
|
458
|
+
|
459
|
+
web_1 | Parameters: {"id"=>"2"}
|
460
|
+
|
461
|
+
web_1 | Post Load (0.6ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 2 LIMIT 1
|
462
|
+
|
463
|
+
web_1 | ↳ app/controllers/likes_controller.rb:15:in `post_params'
|
464
|
+
|
465
|
+
web_1 | User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
|
466
|
+
|
467
|
+
web_1 | ↳ app/controllers/likes_controller.rb:9:in `destroy'
|
468
|
+
|
469
|
+
web_1 | Like Load (0.8ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 1 AND `likes`.`post_id` = 2 LIMIT 1
|
470
|
+
|
471
|
+
web_1 | ↳ app/controllers/likes_controller.rb:9:in `destroy'
|
472
|
+
|
473
|
+
web_1 | (0.3ms) BEGIN
|
474
|
+
|
475
|
+
web_1 | ↳ app/controllers/likes_controller.rb:9:in `destroy'
|
476
|
+
|
477
|
+
web_1 | Like Destroy (0.7ms) DELETE FROM `likes` WHERE `likes`.`id` = 6
|
478
|
+
|
479
|
+
web_1 | ↳ app/controllers/likes_controller.rb:9:in `destroy'
|
480
|
+
|
481
|
+
web_1 | (2.4ms) COMMIT
|
482
|
+
|
483
|
+
web_1 | ↳ app/controllers/likes_controller.rb:9:in `destroy'
|
484
|
+
|
485
|
+
web_1 | Rendering likes/destroy.js.erb
|
486
|
+
|
487
|
+
web_1 | (0.7ms) SELECT COUNT(*) FROM `likes` WHERE `likes`.`post_id` = 2
|
488
|
+
|
489
|
+
web_1 | ↳ app/views/posts/_post.html.erb:3
|
490
|
+
|
491
|
+
web_1 | Like Exists? (0.9ms) SELECT 1 AS one FROM `likes` WHERE `likes`.`user_id` = 1 AND `likes`.`post_id` = 2 LIMIT 1
|
492
|
+
|
493
|
+
web_1 | ↳ app/models/user.rb:16:in `liked_by?'
|
494
|
+
|
495
|
+
web_1 | Rendered posts/_post.html.erb (Duration: 10.5ms | Allocations: 1795)
|
496
|
+
|
497
|
+
web_1 | Rendered likes/destroy.js.erb (Duration: 13.8ms | Allocations: 2045)
|
498
|
+
|
499
|
+
web_1 | Completed 200 OK in 42ms (Views: 15.0ms | ActiveRecord: 7.1ms | Allocations: 6942)
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
```
|
3
エラーログを貼り付けました
test
CHANGED
File without changes
|
test
CHANGED
@@ -435,3 +435,83 @@
|
|
435
435
|
end
|
436
436
|
|
437
437
|
```
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
#エラーログ
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
```
|
446
|
+
|
447
|
+
web_1 | 2021-04-06 01:20:56 +0000: Rack app error handling request { CREATE /like/2 }
|
448
|
+
|
449
|
+
web_1 | #<ActionController::UnknownHttpMethod: CREATE, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH>
|
450
|
+
|
451
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/http/request.rb:431:in `check_method'
|
452
|
+
|
453
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/http/request.rb:143:in `request_method'
|
454
|
+
|
455
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/rack/logger.rb:50:in `started_request_message'
|
456
|
+
|
457
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/rack/logger.rb:36:in `block in call_app'
|
458
|
+
|
459
|
+
web_1 | /usr/local/lib/ruby/2.6.0/logger.rb:465:in `add'
|
460
|
+
|
461
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/logger_thread_safe_level.rb:53:in `add'
|
462
|
+
|
463
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/logger.rb:26:in `block (2 levels) in broadcast'
|
464
|
+
|
465
|
+
web_1 | /usr/local/lib/ruby/2.6.0/logger.rb:525:in `info'
|
466
|
+
|
467
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/rack/logger.rb:36:in `call_app'
|
468
|
+
|
469
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/rack/logger.rb:26:in `block in call'
|
470
|
+
|
471
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/tagged_logging.rb:80:in `block in tagged'
|
472
|
+
|
473
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/tagged_logging.rb:28:in `tagged'
|
474
|
+
|
475
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/tagged_logging.rb:80:in `tagged'
|
476
|
+
|
477
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/rack/logger.rb:26:in `call'
|
478
|
+
|
479
|
+
web_1 | /usr/local/bundle/gems/sprockets-rails-3.2.2/lib/sprockets/rails/quiet_assets.rb:13:in `call'
|
480
|
+
|
481
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
|
482
|
+
|
483
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/middleware/request_id.rb:27:in `call'
|
484
|
+
|
485
|
+
web_1 | /usr/local/bundle/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
|
486
|
+
|
487
|
+
web_1 | /usr/local/bundle/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
|
488
|
+
|
489
|
+
web_1 | /usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
|
490
|
+
|
491
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/middleware/executor.rb:14:in `call'
|
492
|
+
|
493
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/middleware/static.rb:126:in `call'
|
494
|
+
|
495
|
+
web_1 | /usr/local/bundle/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
|
496
|
+
|
497
|
+
web_1 | /usr/local/bundle/gems/actionpack-6.0.3.6/lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
|
498
|
+
|
499
|
+
web_1 | /usr/local/bundle/gems/webpacker-4.3.0/lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
|
500
|
+
|
501
|
+
web_1 | /usr/local/bundle/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
|
502
|
+
|
503
|
+
web_1 | /usr/local/bundle/gems/railties-6.0.3.6/lib/rails/engine.rb:527:in `call'
|
504
|
+
|
505
|
+
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/configuration.rb:227:in `call'
|
506
|
+
|
507
|
+
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:706:in `handle_request'
|
508
|
+
|
509
|
+
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:476:in `process_client'
|
510
|
+
|
511
|
+
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/server.rb:334:in `block in run'
|
512
|
+
|
513
|
+
web_1 | /usr/local/bundle/gems/puma-3.12.6/lib/puma/thread_pool.rb:135:in `block in spawn_thread'
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
```
|
2
コードの修正をしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,11 +4,11 @@
|
|
4
4
|
|
5
5
|
```
|
6
6
|
|
7
|
-
A
|
7
|
+
CREATE 500 internal server rails-ujs.js:216 error
|
8
|
-
|
9
|
-
|
8
|
+
|
10
|
-
|
11
|
-
```
|
9
|
+
```
|
10
|
+
|
11
|
+
とコンソールに出てきます(検証ツール)
|
12
12
|
|
13
13
|
|
14
14
|
|
@@ -30,16 +30,12 @@
|
|
30
30
|
|
31
31
|
Like.create(user_id: current_user.id, post_id: params[:id])
|
32
32
|
|
33
|
-
format.json
|
34
|
-
|
35
33
|
end
|
36
34
|
|
37
35
|
def destroy
|
38
36
|
|
39
37
|
Like.find_by(user_id: current_user.id, post_id: params[:id]).destroy
|
40
38
|
|
41
|
-
format.json
|
42
|
-
|
43
39
|
end
|
44
40
|
|
45
41
|
private
|
@@ -56,18 +52,6 @@
|
|
56
52
|
|
57
53
|
```
|
58
54
|
|
59
|
-
として、javasctiptとして受けとれるようにしてみたのですが、そうしたら
|
60
|
-
|
61
|
-
```
|
62
|
-
|
63
|
-
ArgumentError in LikesController#create
|
64
|
-
|
65
|
-
too few arguments
|
66
|
-
|
67
|
-
```
|
68
|
-
|
69
|
-
となってしまいました。
|
70
|
-
|
71
55
|
|
72
56
|
|
73
57
|
#問題のソースコード
|
@@ -76,19 +60,37 @@
|
|
76
60
|
|
77
61
|
```
|
78
62
|
|
63
|
+
<% if user_signed_in? %>
|
64
|
+
|
65
|
+
<div class="like">
|
66
|
+
|
67
|
+
<h3>いいね件数: <%= post.likes.count %></h3>
|
68
|
+
|
69
|
+
<div class = 'like-button'>
|
70
|
+
|
79
|
-
<% if current_user.liked_by?(post.id) %>
|
71
|
+
<% if current_user.liked_by?(post.id) %>
|
80
|
-
|
72
|
+
|
81
|
-
<td><%= link_to 'いいね外す', destroy_like_path(post), method: :DELETE %></td>
|
73
|
+
<td><%= link_to 'いいねを外す', destroy_like_path(post), class: "like-link", method: :DELETE,remote: true %></td>
|
82
|
-
|
74
|
+
|
83
|
-
<i class="fa fa-heart unlike-btn"></i>
|
75
|
+
<i class="fa fa-heart unlike-btn"></i>
|
84
|
-
|
76
|
+
|
85
|
-
<% else %>
|
77
|
+
<% else %>
|
86
|
-
|
78
|
+
|
87
|
-
<td><%= link_to 'いいね
|
79
|
+
<td><%= link_to 'いいね', create_like_path(post), class: "like-link", method: :create, remote: true %></td>
|
88
|
-
|
80
|
+
|
89
|
-
<i class="fa fa-heart like-btn"></i>
|
81
|
+
<i class="fa fa-heart like-btn"></i>
|
90
|
-
|
82
|
+
|
83
|
+
|
84
|
+
|
91
|
-
<% end %>
|
85
|
+
<% end %>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
</div>
|
90
|
+
|
91
|
+
</div>
|
92
|
+
|
93
|
+
<% end %>
|
92
94
|
|
93
95
|
```
|
94
96
|
|
@@ -134,6 +136,8 @@
|
|
134
136
|
|
135
137
|
```
|
136
138
|
|
139
|
+
|
140
|
+
|
137
141
|
<div class="content-wrapper">
|
138
142
|
|
139
143
|
<div class="content-block">
|
@@ -152,7 +156,7 @@
|
|
152
156
|
|
153
157
|
<% else %>
|
154
158
|
|
155
|
-
<%= image_tag no.user.png %>
|
159
|
+
<%= image_tag 'no.user.png' %>
|
156
160
|
|
157
161
|
<% end %>
|
158
162
|
|
@@ -210,7 +214,7 @@
|
|
210
214
|
|
211
215
|
<div class = 'images'>
|
212
216
|
|
213
|
-
<%= image_tag image %>
|
217
|
+
<%= image_tag image, class: "content-image" %>
|
214
218
|
|
215
219
|
</div>
|
216
220
|
|
@@ -220,67 +224,33 @@
|
|
220
224
|
|
221
225
|
<div class="action-menu">
|
222
226
|
|
223
|
-
<% if user_signed_in? %>
|
224
|
-
|
225
|
-
<div class="like">
|
226
|
-
|
227
|
-
<h3>いいね件数: <%= post.likes.count %></h3>
|
228
|
-
|
229
|
-
</div>
|
230
|
-
|
231
|
-
<% else %>
|
232
|
-
|
233
|
-
<div class="like">
|
234
|
-
|
235
|
-
<h3>いいね件数: <%= post.likes.count %></h3>
|
236
|
-
|
237
|
-
</div>
|
238
|
-
|
239
|
-
<% end %>
|
240
|
-
|
241
|
-
<div class = 'like-f'>
|
242
|
-
|
243
|
-
|
227
|
+
<tr id="post_<%= post.id %>">
|
244
|
-
|
228
|
+
|
245
|
-
|
229
|
+
<%= render 'post', post: post %>
|
246
|
-
|
230
|
+
|
247
|
-
</tr>
|
231
|
+
</tr>
|
248
|
-
|
249
|
-
</div>
|
250
232
|
|
251
233
|
<div class="comment">
|
252
234
|
|
253
|
-
<%if user_signed_in?%>
|
254
|
-
|
255
235
|
<h3>コメント件数: <%= post.comments.count %></h3>
|
256
236
|
|
257
237
|
<%= link_to "コメントする", "/posts/#{post.id}", class: "comment-buttom" %>
|
258
238
|
|
259
|
-
|
239
|
+
</div>
|
240
|
+
|
260
|
-
|
241
|
+
<%if user_signed_in?%>
|
242
|
+
|
243
|
+
<% if current_user.id == post.user.id %>
|
244
|
+
|
261
|
-
|
245
|
+
<%= link_to "編集", edit_post_path(post) %>
|
262
|
-
|
246
|
+
|
263
|
-
|
247
|
+
<%= link_to "削除", post_path(post), method: :delete %>
|
264
|
-
|
248
|
+
|
265
|
-
|
249
|
+
<% end %>
|
266
|
-
|
267
|
-
|
250
|
+
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
<%if user_signed_in?%>
|
272
|
-
|
273
|
-
<% if current_user.id == post.user.id || user_signed_in %>
|
274
|
-
|
275
|
-
<%= link_to "編集", edit_post_path(post) %>
|
276
|
-
|
277
|
-
<%= link_to "削除", post_path(post), method: :delete %>
|
278
|
-
|
279
|
-
<% end %>
|
251
|
+
<% end %>
|
280
|
-
|
281
|
-
|
252
|
+
|
282
|
-
|
283
|
-
|
253
|
+
|
284
254
|
|
285
255
|
</div>
|
286
256
|
|
@@ -298,20 +268,12 @@
|
|
298
268
|
|
299
269
|
<div class="sidebar">
|
300
270
|
|
301
|
-
|
271
|
+
<%= render 'shared/menu'%>
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
272
|
+
|
306
|
-
|
307
|
-
|
273
|
+
|
308
|
-
|
309
|
-
|
310
274
|
|
311
275
|
</div>
|
312
276
|
|
313
|
-
</div>
|
314
|
-
|
315
277
|
</div>
|
316
278
|
|
317
279
|
```
|
@@ -372,8 +334,6 @@
|
|
372
334
|
|
373
335
|
belongs_to :post
|
374
336
|
|
375
|
-
belongs_to :movie
|
376
|
-
|
377
337
|
end
|
378
338
|
|
379
339
|
|
1
初心者マークつけました
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,8 @@
|
|
10
10
|
|
11
11
|
```
|
12
12
|
|
13
|
+
|
14
|
+
|
13
15
|
原因としては、javasctiptで返さないといけないのにHTMLで返されてしまっているからだと考えました。
|
14
16
|
|
15
17
|
なのでいいね機能を実装しているコントローラーを
|