質問編集履歴

8

情報の追加

2020/12/23 11:05

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
+ # [このアプリのgithub](https://github.com/Harasou21/coffee_passport/tree/liking)
14
+
15
+
16
+
13
17
 
14
18
 
15
19
 

7

タイトルの修正

2020/12/23 11:05

投稿

divclass123
divclass123

スコア35

test CHANGED
@@ -1 +1 @@
1
- いいねボタンを押してもいいね変わらない
1
+ いいねの解除できない
test CHANGED
File without changes

6

最新のコードへ変更

2020/12/23 08:08

投稿

divclass123
divclass123

スコア35

test CHANGED
@@ -1 +1 @@
1
- いいね機能上手く実装できない
1
+ いいねボタンを押してもいいね変わらない
test CHANGED
@@ -10,15 +10,17 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
15
+
16
+
13
17
  ### 発生している問題・エラーメッセージ
14
18
 
15
19
  ![イメージ説明](e02c5f8f24186d8fb72d36e1621feba0.png)
16
20
 
17
- ![![イメージ説明](7e70671e58f9e76823ab5681540d426e.png)](ba7333a3e628cef93a831ca2131bb6dc.png)
21
+
18
-
19
-
20
-
22
+
21
- ここのいいねボタンを押した時にコンソールで画像ようなエラーきました
23
+ # いいねボタンを押した時、いいね上手く行くです、いいねの解除が上手くいきません
22
24
 
23
25
 
24
26
 
@@ -28,49 +30,57 @@
28
30
 
29
31
  ```
30
32
 
33
+ Started DELETE "/like/19" for ::1 at 2020-12-23 16:56:49 +0900
34
+
35
+ Processing by LikesController#unlike as JS
36
+
37
+ Parameters: {"drink_id"=>"19"}
38
+
39
+ Drink Load (0.5ms) SELECT `drinks`.* FROM `drinks` WHERE `drinks`.`id` = 19 LIMIT 1
40
+
41
+ ↳ app/controllers/likes_controller.rb:21:in `set_variables'
42
+
43
+ User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 6 LIMIT 1
44
+
45
+ ↳ app/helpers/sessions_helper.rb:53:in `current_user'
46
+
47
+ Like Load (0.3ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 6 AND `likes`.`drink_id` = 19 LIMIT 1
48
+
49
+ ↳ app/controllers/likes_controller.rb:14:in `unlike'
50
+
31
- app/views/likes/like.js.erb:1
51
+ Rendering likes/unlike.js.erb
32
-
33
- Rendered likes/_like.html.erb (Duration: 464.9ms | Allocations: 199855)
52
+
34
-
35
- Rendered likes/like.js.erb (Duration: 465.6ms | Allocations: 200200)
36
-
37
- Completed 500 Internal Server Error in 14458ms (ActiveRecord: 2.9ms | Allocations: 220526)
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
- ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff892254698>:0x00007ff8ace00bd0>
46
-
47
- Did you mean? @drink
48
-
49
- @drinks):
50
-
51
- 1: <div class="like" id="like-link-<%= drink.id %>">
52
-
53
- 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
54
-
55
- 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
56
-
57
- 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
53
+ CACHE Like Load (0.0ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 6 AND `likes`.`drink_id` = 19 LIMIT 1
58
-
59
-
60
-
54
+
61
- app/views/likes/_like.html.erb:1
55
+ app/views/likes/_like.html.erb:2
56
+
62
-
57
+ (0.3ms) SELECT COUNT(*) FROM `likes` WHERE `likes`.`drink_id` = 19
58
+
63
- app/views/likes/like.js.erb:1
59
+ app/views/likes/_like.html.erb:4
60
+
64
-
61
+ Rendered likes/_like.html.erb (Duration: 3.2ms | Allocations: 1654)
62
+
63
+ Rendered likes/unlike.js.erb (Duration: 3.5ms | Allocations: 1762)
64
+
65
+ Completed 200 OK in 11ms (Views: 4.1ms | ActiveRecord: 1.5ms | Allocations: 4365)
66
+
67
+
68
+
69
+
70
+
65
- ```
71
+ ```
66
-
67
-
68
-
72
+
73
+
74
+
69
- またFailed to load resource: the server responded with a status of 500 (Internal Server Error)というエラーもコンール上で確認できした。
75
+ しっかり、いいねを取り消すunlikeッドが反応して
70
-
71
-
72
-
76
+
77
+
78
+
73
- このようなエラーがおきした
79
+ コンソール上ではエラーは確認されてせん
80
+
81
+
82
+
83
+ きっとレンダリングが上手く行ってない可能性が高いと思います
74
84
 
75
85
 
76
86
 
@@ -162,7 +172,7 @@
162
172
 
163
173
  </div>
164
174
 
165
- <%= render 'likes/like', locals: {drink: drink}%>
175
+ <%= render partial: "likes/like",locals:{drink: drink}%>
166
176
 
167
177
  </div>
168
178
 
@@ -218,250 +228,194 @@
218
228
 
219
229
  ```erb
220
230
 
231
+ $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like", drink: @drink )) %>');
232
+
233
+ ```
234
+
235
+
236
+
237
+ **likes/unlike.js.erb**
238
+
239
+
240
+
241
+ ```erb
242
+
243
+ $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like", drink: @drink )) %>');
244
+
245
+ ```
246
+
247
+
248
+
249
+ こっちが上手くいかない。
250
+
251
+
252
+
253
+ **like_controller**
254
+
255
+
256
+
257
+ ```ruby
258
+
259
+ class LikesController < ApplicationController
260
+
261
+ include SessionsHelper
262
+
263
+
264
+
265
+ before_action :set_variables
266
+
267
+ def like
268
+
269
+
270
+
271
+ like = current_user.likes.new(drink_id: @drink.id)
272
+
273
+ #redirect_to drinks_path
274
+
275
+ # jsを用いるので画面遷移は行わない
276
+
277
+ binding.pry
278
+
279
+ like.save
280
+
281
+ end
282
+
283
+
284
+
285
+ def unlike
286
+
287
+ like = current_user.likes.find_by(drink_id: @drink.id)
288
+
289
+ binding.pry
290
+
291
+ like.destroy
292
+
293
+ end
294
+
295
+
296
+
297
+ private
298
+
299
+
300
+
301
+ def set_variables
302
+
303
+ @drink = Drink.find(params[:drink_id])
304
+
305
+ @id_name = "#like-link-#{@drink.id}"
306
+
307
+ end
308
+
309
+
310
+
311
+ end
312
+
313
+
314
+
315
+ ```
316
+
317
+
318
+
319
+ **routes.rb**
320
+
321
+
322
+
323
+ ```ruby
324
+
325
+ Rails.application.routes.draw do
326
+
327
+ root to: 'drinks#index'
328
+
329
+ get '/login', to: 'sessions#new'
330
+
331
+ post '/login', to: 'sessions#create'
332
+
333
+ delete '/logout', to: 'sessions#destroy'
334
+
335
+ get 'user/likes', to: 'users#likes'
336
+
337
+ resources :users do
338
+
339
+ member do
340
+
341
+ get :following,:followers
342
+
343
+ # memberメソッドを使うと
344
+
345
+ # ユーザーidが含まれてるURlを扱うようになる
346
+
347
+ end
348
+
349
+ end
350
+
351
+ get '/drinks/searchdrink', to: 'drinks#search_drink'
352
+
353
+ resources :drinks, only: [:index,:new,:show,:create,:destroy] do
354
+
355
+ collection do
356
+
357
+ get 'search'
358
+
359
+ end
360
+
361
+
362
+
363
+ resources :comments,only: :create
364
+
365
+ # あるツイートに対してのコメントと言う
366
+
367
+ # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
368
+
369
+ # パスになります
370
+
371
+
372
+
373
+ resources :trades,only: [:index,:new,:create]
374
+
375
+ end
376
+
377
+ post '/drinks/:id/trades/new', to: 'trades#create'
378
+
379
+ resources :relationships, only: [:create,:destroy]
380
+
381
+ post 'like/:drink_id' ,to: 'likes#like', as: 'like'
382
+
383
+ delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
384
+
385
+ end
386
+
387
+
388
+
389
+ ```
390
+
391
+
392
+
393
+ ### 試したこと
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+ とあったので、drinkの部分を@drinkに全部
402
+
403
+
404
+
405
+ ```erb
406
+
221
407
  $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
222
408
 
223
409
  ```
224
410
 
225
411
 
226
412
 
227
- **likes/unlike.js.erb**
413
+ に元々
228
414
 
229
415
 
230
416
 
231
417
  ```erb
232
418
 
233
- $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
234
-
235
- ```
236
-
237
-
238
-
239
- **like_controller**
240
-
241
-
242
-
243
- ```ruby
244
-
245
- class LikesController < ApplicationController
246
-
247
- include SessionsHelper
248
-
249
-
250
-
251
- before_action :set_variables
252
-
253
- def like
254
-
255
-
256
-
257
- like = current_user.likes.new(drink_id: @drink.id)
258
-
259
- #redirect_to drinks_path
260
-
261
- # jsを用いるので画面遷移は行わない
262
-
263
- binding.pry
264
-
265
- like.save
266
-
267
- end
268
-
269
-
270
-
271
- def unlike
272
-
273
- like = current_user.likes.find_by(drink_id: @drink.id)
274
-
275
- binding.pry
276
-
277
- like.destroy
278
-
279
- end
280
-
281
-
282
-
283
- private
284
-
285
-
286
-
287
- def set_variables
288
-
289
- @drink = Drink.find(params[:drink_id])
290
-
291
- @id_name = "#like-link-#{@drink.id}"
292
-
293
- end
294
-
295
-
296
-
297
- end
298
-
299
-
300
-
301
- ```
302
-
303
-
304
-
305
- **routes.rb**
306
-
307
-
308
-
309
- ```ruby
310
-
311
- Rails.application.routes.draw do
312
-
313
- root to: 'drinks#index'
314
-
315
- get '/login', to: 'sessions#new'
316
-
317
- post '/login', to: 'sessions#create'
318
-
319
- delete '/logout', to: 'sessions#destroy'
320
-
321
- get 'user/likes', to: 'users#likes'
322
-
323
- resources :users do
324
-
325
- member do
326
-
327
- get :following,:followers
328
-
329
- # memberメソッドを使うと
330
-
331
- # ユーザーidが含まれてるURlを扱うようになる
332
-
333
- end
334
-
335
- end
336
-
337
- get '/drinks/searchdrink', to: 'drinks#search_drink'
338
-
339
- resources :drinks, only: [:index,:new,:show,:create,:destroy] do
340
-
341
- collection do
342
-
343
- get 'search'
344
-
345
- end
346
-
347
-
348
-
349
- resources :comments,only: :create
350
-
351
- # あるツイートに対してのコメントと言う
352
-
353
- # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
354
-
355
- # パスになります
356
-
357
-
358
-
359
- resources :trades,only: [:index,:new,:create]
360
-
361
- end
362
-
363
- post '/drinks/:id/trades/new', to: 'trades#create'
364
-
365
- resources :relationships, only: [:create,:destroy]
366
-
367
- post 'like/:drink_id' ,to: 'likes#like', as: 'like'
368
-
369
- delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
370
-
371
- end
372
-
373
-
374
-
375
- ```
376
-
377
-
378
-
379
- ### 試したこと
380
-
381
-
382
-
383
- **rails serverのlikes/_like.html.erbで**
384
-
385
-
386
-
387
- ```
388
-
389
- ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
390
-
391
- Did you mean? @drink
392
-
393
- @drinks):
394
-
395
- 1: <div class="like" id="like-link-<%= drink.id %>">
396
-
397
- 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
398
-
399
- 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
400
-
401
- 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
402
-
403
-
404
-
405
- app/views/likes/_like.html.erb:1
406
-
407
- app/views/likes/unlike.js.erb:1
408
-
409
-
410
-
411
-
412
-
413
- ```
414
-
415
-
416
-
417
- とあったので、drinkの部分を@drinkに全部変更したら、
418
-
419
- undefined method `id' for nil:NilClass
420
-
421
- とエラーが出ました
422
-
423
-
424
-
425
- パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
426
-
427
-
428
-
429
- drinkも
430
-
431
- **drinks/index.html.erb**
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
- ```erb
440
-
441
- <%= render 'likes/like',locals:{ drink: drink}%>
442
-
443
- ```
444
-
445
-
446
-
447
- とdrinkをしっかり定義しているので原因がわかりません。
448
-
449
-
450
-
451
- ```erb
452
-
453
- $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
454
-
455
- ```
456
-
457
-
458
-
459
- に元々
460
-
461
-
462
-
463
- ```erb
464
-
465
419
  $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
466
420
 
467
421
  ```

5

情報の追加

2020/12/23 08:03

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -218,233 +218,263 @@
218
218
 
219
219
  ```erb
220
220
 
221
+ $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
222
+
223
+ ```
224
+
225
+
226
+
227
+ **likes/unlike.js.erb**
228
+
229
+
230
+
231
+ ```erb
232
+
233
+ $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
234
+
235
+ ```
236
+
237
+
238
+
239
+ **like_controller**
240
+
241
+
242
+
243
+ ```ruby
244
+
245
+ class LikesController < ApplicationController
246
+
247
+ include SessionsHelper
248
+
249
+
250
+
251
+ before_action :set_variables
252
+
253
+ def like
254
+
255
+
256
+
257
+ like = current_user.likes.new(drink_id: @drink.id)
258
+
259
+ #redirect_to drinks_path
260
+
261
+ # jsを用いるので画面遷移は行わない
262
+
263
+ binding.pry
264
+
265
+ like.save
266
+
267
+ end
268
+
269
+
270
+
271
+ def unlike
272
+
273
+ like = current_user.likes.find_by(drink_id: @drink.id)
274
+
275
+ binding.pry
276
+
277
+ like.destroy
278
+
279
+ end
280
+
281
+
282
+
283
+ private
284
+
285
+
286
+
287
+ def set_variables
288
+
289
+ @drink = Drink.find(params[:drink_id])
290
+
291
+ @id_name = "#like-link-#{@drink.id}"
292
+
293
+ end
294
+
295
+
296
+
297
+ end
298
+
299
+
300
+
301
+ ```
302
+
303
+
304
+
305
+ **routes.rb**
306
+
307
+
308
+
309
+ ```ruby
310
+
311
+ Rails.application.routes.draw do
312
+
313
+ root to: 'drinks#index'
314
+
315
+ get '/login', to: 'sessions#new'
316
+
317
+ post '/login', to: 'sessions#create'
318
+
319
+ delete '/logout', to: 'sessions#destroy'
320
+
321
+ get 'user/likes', to: 'users#likes'
322
+
323
+ resources :users do
324
+
325
+ member do
326
+
327
+ get :following,:followers
328
+
329
+ # memberメソッドを使うと
330
+
331
+ # ユーザーidが含まれてるURlを扱うようになる
332
+
333
+ end
334
+
335
+ end
336
+
337
+ get '/drinks/searchdrink', to: 'drinks#search_drink'
338
+
339
+ resources :drinks, only: [:index,:new,:show,:create,:destroy] do
340
+
341
+ collection do
342
+
343
+ get 'search'
344
+
345
+ end
346
+
347
+
348
+
349
+ resources :comments,only: :create
350
+
351
+ # あるツイートに対してのコメントと言う
352
+
353
+ # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
354
+
355
+ # パスになります
356
+
357
+
358
+
359
+ resources :trades,only: [:index,:new,:create]
360
+
361
+ end
362
+
363
+ post '/drinks/:id/trades/new', to: 'trades#create'
364
+
365
+ resources :relationships, only: [:create,:destroy]
366
+
367
+ post 'like/:drink_id' ,to: 'likes#like', as: 'like'
368
+
369
+ delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
370
+
371
+ end
372
+
373
+
374
+
375
+ ```
376
+
377
+
378
+
379
+ ### 試したこと
380
+
381
+
382
+
383
+ **rails serverのlikes/_like.html.erbで**
384
+
385
+
386
+
387
+ ```
388
+
389
+ ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
390
+
391
+ Did you mean? @drink
392
+
393
+ @drinks):
394
+
395
+ 1: <div class="like" id="like-link-<%= drink.id %>">
396
+
397
+ 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
398
+
399
+ 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
400
+
401
+ 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
402
+
403
+
404
+
405
+ app/views/likes/_like.html.erb:1
406
+
407
+ app/views/likes/unlike.js.erb:1
408
+
409
+
410
+
411
+
412
+
413
+ ```
414
+
415
+
416
+
417
+ とあったので、drinkの部分を@drinkに全部変更したら、
418
+
419
+ undefined method `id' for nil:NilClass
420
+
421
+ とエラーが出ました
422
+
423
+
424
+
425
+ パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
426
+
427
+
428
+
429
+ drinkも
430
+
431
+ **drinks/index.html.erb**
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+ ```erb
440
+
441
+ <%= render 'likes/like',locals:{ drink: drink}%>
442
+
443
+ ```
444
+
445
+
446
+
447
+ とdrinkをしっかり定義しているので原因がわかりません。
448
+
449
+
450
+
451
+ ```erb
452
+
453
+ $(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
454
+
455
+ ```
456
+
457
+
458
+
459
+ に元々
460
+
461
+
462
+
463
+ ```erb
464
+
221
465
  $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
222
466
 
223
467
  ```
224
468
 
225
-
226
-
227
- **likes/unlike.js.erb**
228
-
229
-
230
-
231
- ```erb
469
+ このように
232
-
233
- $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
470
+
234
-
235
- ```
471
+ ダブルクォーテーションを入れてたのですが、
236
-
237
-
238
-
472
+
239
- **like_controller**
473
+ likes_contoroller
240
-
241
-
242
-
243
- ```ruby
474
+
244
-
245
- class LikesController < ApplicationController
246
-
247
- include SessionsHelper
248
-
249
-
250
-
251
- before_action :set_variables
252
-
253
- def like
254
-
255
-
256
-
257
- like = current_user.likes.new(drink_id: @drink.id)
258
-
259
- #redirect_to drinks_path
260
-
261
- # jsを用いるので画面遷移は行わない
262
-
263
- binding.pry
264
-
265
- like.save
266
-
267
- end
268
-
269
-
270
-
271
- def unlike
272
-
273
- like = current_user.likes.find_by(drink_id: @drink.id)
274
-
275
- binding.pry
276
-
277
- like.destroy
278
-
279
- end
280
-
281
-
282
-
283
- private
284
-
285
-
286
-
287
- def set_variables
288
-
289
- @drink = Drink.find(params[:drink_id])
290
-
291
- @id_name = "#like-link-#{@drink.id}"
475
+ @id_name = "#like-link-#{@drink.id}"
292
-
293
- end
476
+
294
-
295
-
296
-
297
- end
298
-
299
-
300
-
301
- ```
302
-
303
-
304
-
305
- **routes.rb**
306
-
307
-
308
-
309
- ```ruby
310
-
311
- Rails.application.routes.draw do
312
-
313
- root to: 'drinks#index'
314
-
315
- get '/login', to: 'sessions#new'
316
-
317
- post '/login', to: 'sessions#create'
318
-
319
- delete '/logout', to: 'sessions#destroy'
320
-
321
- get 'user/likes', to: 'users#likes'
322
-
323
- resources :users do
324
-
325
- member do
326
-
327
- get :following,:followers
328
-
329
- # memberメソッドを使うと
330
-
331
- # ユーザーidが含まれてるURlを扱うようになる
332
-
333
- end
334
-
335
- end
336
-
337
- get '/drinks/searchdrink', to: 'drinks#search_drink'
338
-
339
- resources :drinks, only: [:index,:new,:show,:create,:destroy] do
340
-
341
- collection do
342
-
343
- get 'search'
344
-
345
- end
346
-
347
-
348
-
349
- resources :comments,only: :create
350
-
351
- # あるツイートに対してのコメントと言う
352
-
353
- # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
354
-
355
- # パスになります
356
-
357
-
358
-
359
- resources :trades,only: [:index,:new,:create]
360
-
361
- end
362
-
363
- post '/drinks/:id/trades/new', to: 'trades#create'
364
-
365
- resources :relationships, only: [:create,:destroy]
366
-
367
- post 'like/:drink_id' ,to: 'likes#like', as: 'like'
368
-
369
- delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
370
-
371
- end
372
-
373
-
374
-
375
- ```
376
-
377
-
378
-
379
- ### 試したこと
380
-
381
-
382
-
383
- **rails serverのlikes/_like.html.erbで**
384
-
385
-
386
-
387
- ```
388
-
389
- ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
390
-
391
- Did you mean? @drink
392
-
393
- @drinks):
394
-
395
- 1: <div class="like" id="like-link-<%= drink.id %>">
396
-
397
- 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
398
-
399
- 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
400
-
401
- 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
402
-
403
-
404
-
405
- app/views/likes/_like.html.erb:1
406
-
407
- app/views/likes/unlike.js.erb:1
408
-
409
-
410
-
411
-
412
-
413
- ```
414
-
415
-
416
-
417
- あったので、drinkの部分@drinkに全部したら、
477
+ 定義してたので、ダブルクォーテーション外しても結果はわりませんでした
418
-
419
- undefined method `id' for nil:NilClass
420
-
421
- とエラーが出ました
422
-
423
-
424
-
425
- パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
426
-
427
-
428
-
429
- drinkも
430
-
431
- **drinks/index.html.erb**
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
- ```erb
440
-
441
- <%= render 'likes/like',locals:{ drink: drink}%>
442
-
443
- ```
444
-
445
-
446
-
447
- とdrinkをしっかり定義しているので原因がわかりません。
448
478
 
449
479
 
450
480
 

4

情報の追加

2020/12/23 07:05

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -446,4 +446,12 @@
446
446
 
447
447
  とdrinkをしっかり定義しているので原因がわかりません。
448
448
 
449
+
450
+
451
+ また、binding.pry(デバッガーツール)で
452
+
453
+ likes_contorollerのiike,unlikeアクションはしっかり反応して、処理を止めてくれて、変数の中身にしっかり値が入ってくれました。
454
+
455
+
456
+
449
457
   御回答いただければ幸いです。

3

情報の追加

2020/12/23 06:56

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -386,13 +386,29 @@
386
386
 
387
387
  ```
388
388
 
389
- ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007fce4fa14a08>:0x00007fce4fa1c8e8>
389
+ ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
390
-
390
+
391
- Did you mean? @drink):
391
+ Did you mean? @drink
392
+
393
+ @drinks):
392
394
 
393
395
  1: <div class="like" id="like-link-<%= drink.id %>">
394
396
 
395
- 2: <% if current_user.likes.find_by(drink_id: drin
397
+ 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
398
+
399
+ 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
400
+
401
+ 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
402
+
403
+
404
+
405
+ app/views/likes/_like.html.erb:1
406
+
407
+ app/views/likes/unlike.js.erb:1
408
+
409
+
410
+
411
+
396
412
 
397
413
  ```
398
414
 

2

最新のコードへ変更

2020/12/23 06:24

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -250,7 +250,7 @@
250
250
 
251
251
  before_action :set_variables
252
252
 
253
- def create
253
+ def like
254
254
 
255
255
 
256
256
 
@@ -268,7 +268,7 @@
268
268
 
269
269
 
270
270
 
271
- def destroy
271
+ def unlike
272
272
 
273
273
  like = current_user.likes.find_by(drink_id: @drink.id)
274
274
 

1

最新のコードへ変更

2020/12/23 06:19

投稿

divclass123
divclass123

スコア35

test CHANGED
File without changes
test CHANGED
@@ -28,15 +28,13 @@
28
28
 
29
29
  ```
30
30
 
31
- ↳ app/controllers/likes_controller.rb:23:in `set_variables'
32
-
33
- Rendering likes/like.js.erb
31
+ app/views/likes/like.js.erb:1
34
-
32
+
35
- Rendered likes/_like.html.erb (Duration: 277.6ms | Allocations: 178332)
33
+ Rendered likes/_like.html.erb (Duration: 464.9ms | Allocations: 199855)
36
-
34
+
37
- Rendered likes/like.js.erb (Duration: 278.8ms | Allocations: 178682)
35
+ Rendered likes/like.js.erb (Duration: 465.6ms | Allocations: 200200)
38
-
36
+
39
- Completed 500 Internal Server Error in 286ms (ActiveRecord: 0.4ms | Allocations: 181026)
37
+ Completed 500 Internal Server Error in 14458ms (ActiveRecord: 2.9ms | Allocations: 220526)
40
38
 
41
39
 
42
40
 
@@ -44,384 +42,386 @@
44
42
 
45
43
 
46
44
 
45
+ ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff892254698>:0x00007ff8ace00bd0>
46
+
47
+ Did you mean? @drink
48
+
49
+ @drinks):
50
+
51
+ 1: <div class="like" id="like-link-<%= drink.id %>">
52
+
53
+ 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
54
+
55
+ 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
56
+
57
+ 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
58
+
59
+
60
+
61
+ app/views/likes/_like.html.erb:1
62
+
63
+ app/views/likes/like.js.erb:1
64
+
65
+ ```
66
+
67
+
68
+
69
+ またFailed to load resource: the server responded with a status of 500 (Internal Server Error)というエラーもコンソール上で確認できました。
70
+
71
+
72
+
73
+ このようなエラーがおきました
74
+
75
+
76
+
77
+ ### 該当のソースコード
78
+
79
+
80
+
81
+ **drinks/index.erb**
82
+
83
+
84
+
85
+ ```ruby
86
+
87
+ <% @drinks.each do |drink|%>
88
+
89
+ <li class='list'>
90
+
91
+ <%= link_to drink_path(drink.id) do %>
92
+
93
+ <%= link_to user_path(drink.user.id) do%>
94
+
95
+ <div class="user-info-timeline">
96
+
97
+ <%=image_tag drink.user.image.variant(resize: '60x60'),class: "user-img-timeline" if drink.user.image.attached?%>
98
+
99
+ <div class="username-timeline">
100
+
101
+ <%= drink.user.nickname %>
102
+
103
+ </div>
104
+
105
+ </div>
106
+
107
+ <% end %>
108
+
109
+ <div class='item-img-content'>
110
+
111
+ <%= image_tag drink.image , class: "item-img" if drink.image.attached? %>
112
+
113
+
114
+
115
+ <%# if drink.trade%>
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+ <%# end %>
124
+
125
+ </div>
126
+
127
+ <div class='item-info'>
128
+
129
+ <h3 class='item-name'>
130
+
131
+ <%= drink.name %>
132
+
133
+ </h3>
134
+
135
+ <div class='item-price'>
136
+
137
+ <span><%= drink.price %>円<br>(税込み)</span>
138
+
139
+ <div class='star-btn'>
140
+
141
+ <%# image_tag "star.png", class:"star-icon" %>
142
+
143
+ <span class='star-count'>0</span>
144
+
145
+ </div>
146
+
147
+ </div>
148
+
149
+ <div class='item-explain'>
150
+
151
+ <%= drink.explain%>
152
+
153
+ </div>
154
+
155
+ <div class='item-tag'>
156
+
157
+ <% drink.tags.each do |tag| %>
158
+
159
+ #<%=tag.tag_name%>
160
+
161
+ <%end%>
162
+
163
+ </div>
164
+
165
+ <%= render 'likes/like', locals: {drink: drink}%>
166
+
167
+ </div>
168
+
169
+
170
+
171
+ <% end %>
172
+
173
+ ```
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ **likes/_like.html.erb**
182
+
183
+
184
+
185
+ ```ruby
186
+
187
+ <div class="like" id="like-link-<%= drink.id %>">
188
+
189
+ <% if current_user.likes.find_by(drink_id: drink.id) %>
190
+
191
+ <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
192
+
193
+ <div class = "iine__button">❤️<%= drink.likes.count %></div>
194
+
195
+ <% end %>
196
+
197
+ <% else %>
198
+
199
+ <%= link_to like_path(drink.id), method: :post, remote: true do %>
200
+
201
+ <div class = "iine__button">♡️<%= drink.likes.count %></div>
202
+
203
+ <% end %>
204
+
205
+ <% end %>
206
+
207
+ </div>
208
+
209
+
210
+
211
+ ```
212
+
213
+
214
+
215
+ **likes/like.js.erb**
216
+
217
+
218
+
219
+ ```erb
220
+
221
+ $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
222
+
223
+ ```
224
+
225
+
226
+
227
+ **likes/unlike.js.erb**
228
+
229
+
230
+
231
+ ```erb
232
+
233
+ $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
234
+
235
+ ```
236
+
237
+
238
+
239
+ **like_controller**
240
+
241
+
242
+
243
+ ```ruby
244
+
245
+ class LikesController < ApplicationController
246
+
247
+ include SessionsHelper
248
+
249
+
250
+
251
+ before_action :set_variables
252
+
253
+ def create
254
+
255
+
256
+
257
+ like = current_user.likes.new(drink_id: @drink.id)
258
+
259
+ #redirect_to drinks_path
260
+
261
+ # jsを用いるので画面遷移は行わない
262
+
263
+ binding.pry
264
+
265
+ like.save
266
+
267
+ end
268
+
269
+
270
+
271
+ def destroy
272
+
273
+ like = current_user.likes.find_by(drink_id: @drink.id)
274
+
275
+ binding.pry
276
+
277
+ like.destroy
278
+
279
+ end
280
+
281
+
282
+
283
+ private
284
+
285
+
286
+
287
+ def set_variables
288
+
289
+ @drink = Drink.find(params[:drink_id])
290
+
291
+ @id_name = "#like-link-#{@drink.id}"
292
+
293
+ end
294
+
295
+
296
+
297
+ end
298
+
299
+
300
+
301
+ ```
302
+
303
+
304
+
305
+ **routes.rb**
306
+
307
+
308
+
309
+ ```ruby
310
+
311
+ Rails.application.routes.draw do
312
+
313
+ root to: 'drinks#index'
314
+
315
+ get '/login', to: 'sessions#new'
316
+
317
+ post '/login', to: 'sessions#create'
318
+
319
+ delete '/logout', to: 'sessions#destroy'
320
+
321
+ get 'user/likes', to: 'users#likes'
322
+
323
+ resources :users do
324
+
325
+ member do
326
+
327
+ get :following,:followers
328
+
329
+ # memberメソッドを使うと
330
+
331
+ # ユーザーidが含まれてるURlを扱うようになる
332
+
333
+ end
334
+
335
+ end
336
+
337
+ get '/drinks/searchdrink', to: 'drinks#search_drink'
338
+
339
+ resources :drinks, only: [:index,:new,:show,:create,:destroy] do
340
+
341
+ collection do
342
+
343
+ get 'search'
344
+
345
+ end
346
+
347
+
348
+
349
+ resources :comments,only: :create
350
+
351
+ # あるツイートに対してのコメントと言う
352
+
353
+ # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
354
+
355
+ # パスになります
356
+
357
+
358
+
359
+ resources :trades,only: [:index,:new,:create]
360
+
361
+ end
362
+
363
+ post '/drinks/:id/trades/new', to: 'trades#create'
364
+
365
+ resources :relationships, only: [:create,:destroy]
366
+
367
+ post 'like/:drink_id' ,to: 'likes#like', as: 'like'
368
+
369
+ delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
370
+
371
+ end
372
+
373
+
374
+
375
+ ```
376
+
377
+
378
+
379
+ ### 試したこと
380
+
381
+
382
+
383
+ **rails serverのlikes/_like.html.erbで**
384
+
385
+
386
+
387
+ ```
388
+
47
389
  ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007fce4fa14a08>:0x00007fce4fa1c8e8>
48
390
 
49
391
  Did you mean? @drink):
50
392
 
51
393
  1: <div class="like" id="like-link-<%= drink.id %>">
52
394
 
53
- 2: <% if current_user.likes.find_by(drink_id: drink.id) %>
54
-
55
- 3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
56
-
57
- 4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
58
-
59
-
60
-
61
- app/views/likes/_like.html.erb:1
62
-
63
- app/views/likes/like.js.erb:1
64
-
65
- ```
66
-
67
-
68
-
69
- またrails-ujs.js:216 POST http://localhost:3000/like/26 500 (Internal Server Error)というエラーもコンソール上で確認できました。
70
-
71
-
72
-
73
- このようなエラーがおきました
74
-
75
-
76
-
77
- ### 該当のソースコード
78
-
79
-
80
-
81
- **drinks/index.erb**
82
-
83
-
84
-
85
- ```ruby
86
-
87
- <% @drinks.each do |drink|%>
88
-
89
- <li class='list'>
90
-
91
- <%= link_to drink_path(drink.id) do %>
92
-
93
- <%= link_to user_path(drink.user.id) do%>
94
-
95
- <div class="user-info-timeline">
96
-
97
- <%=image_tag drink.user.image.variant(resize: '60x60'),class: "user-img-timeline" if drink.user.image.attached?%>
98
-
99
- <div class="username-timeline">
100
-
101
- <%= drink.user.nickname %>
102
-
103
- </div>
104
-
105
- </div>
106
-
107
- <% end %>
108
-
109
- <div class='item-img-content'>
110
-
111
- <%= image_tag drink.image , class: "item-img" if drink.image.attached? %>
112
-
113
-
114
-
115
- <%# if drink.trade%>
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
- <%# end %>
124
-
125
- </div>
126
-
127
- <div class='item-info'>
128
-
129
- <h3 class='item-name'>
130
-
131
- <%= drink.name %>
132
-
133
- </h3>
134
-
135
- <div class='item-price'>
136
-
137
- <span><%= drink.price %>円<br>(税込み)</span>
138
-
139
- <div class='star-btn'>
140
-
141
- <%# image_tag "star.png", class:"star-icon" %>
142
-
143
- <span class='star-count'>0</span>
144
-
145
- </div>
146
-
147
- </div>
148
-
149
- <div class='item-explain'>
150
-
151
- <%= drink.explain%>
152
-
153
- </div>
154
-
155
- <div class='item-tag'>
156
-
157
- <% drink.tags.each do |tag| %>
158
-
159
- #<%=tag.tag_name%>
160
-
161
- <%end%>
162
-
163
- </div>
164
-
165
- <%= render 'likes/like', locals: {drink: drink}%>
166
-
167
- </div>
168
-
169
-
170
-
171
- <% end %>
172
-
173
- ```
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
- **likes/_like.html.erb**
182
-
183
-
184
-
185
- ```ruby
186
-
187
- <div class="like" id="like-link-<%= drink.id %>">
188
-
189
- <% if current_user.likes.find_by(drink_id: drink.id) %>
190
-
191
- <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
192
-
193
- <div class = "iine__button">❤️<%= drink.likes.count %></div>
194
-
195
- <% end %>
196
-
197
- <% else %>
198
-
199
- <%= link_to like_path(drink.id), method: :post, remote: true do %>
200
-
201
- <div class = "iine__button">♡️<%= drink.likes.count %></div>
202
-
203
- <% end %>
204
-
205
- <% end %>
206
-
207
- </div>
208
-
209
-
210
-
211
- ```
212
-
213
-
214
-
215
- **likes/like.js.erb**
395
+ 2: <% if current_user.likes.find_by(drink_id: drin
396
+
397
+ ```
398
+
399
+
400
+
401
+ とあったので、drinkの部分を@drinkに全部変更したら、
402
+
403
+ undefined method `id' for nil:NilClass
404
+
405
+ とエラーが出ました
406
+
407
+
408
+
409
+ パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
410
+
411
+
412
+
413
+ drinkも
414
+
415
+ **drinks/index.html.erb**
416
+
417
+
418
+
419
+
216
420
 
217
421
 
218
422
 
219
423
  ```erb
220
424
 
221
- $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
222
-
223
- ```
224
-
225
-
226
-
227
- **likes/unlike.js.erb**
228
-
229
-
230
-
231
- ```erb
232
-
233
- $("<%= @id_name %>").html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
234
-
235
- ```
236
-
237
-
238
-
239
- **like_controller**
240
-
241
-
242
-
243
- ```ruby
244
-
245
- class LikesController < ApplicationController
246
-
247
- include SessionsHelper
248
-
249
-
250
-
251
- before_action :set_variables
252
-
253
- def create
254
-
255
-
256
-
257
- like = current_user.likes.new(drink_id: drink.id)
258
-
259
- #redirect_to drinks_path
260
-
261
- # jsを用いるので画面遷移は行わない
262
-
263
- binding.pry
264
-
265
- like.save
266
-
267
- end
268
-
269
-
270
-
271
- def destroy
272
-
273
- like = current_user.likes.find_by(drink_id: drink.id)
274
-
275
- binding.pry
276
-
277
- like.destroy
278
-
279
- end
280
-
281
-
282
-
283
- private
284
-
285
-
286
-
287
- def set_variables
288
-
289
- @drink = Drink.find(params[:drink_id])
290
-
291
- @id_name = "#like-link-#{@drink.id}"
292
-
293
- end
294
-
295
-
296
-
297
- end
298
-
299
-
300
-
301
- ```
302
-
303
-
304
-
305
- **routes.rb**
306
-
307
-
308
-
309
- ```ruby
310
-
311
- Rails.application.routes.draw do
312
-
313
- root to: 'drinks#index'
314
-
315
- get '/login', to: 'sessions#new'
316
-
317
- post '/login', to: 'sessions#create'
318
-
319
- delete '/logout', to: 'sessions#destroy'
320
-
321
- get 'user/likes', to: 'users#likes'
322
-
323
- resources :users do
324
-
325
- member do
326
-
327
- get :following,:followers
328
-
329
- # memberメソッドを使うと
330
-
331
- # ユーザーidが含まれてるURlを扱うようになる
332
-
333
- end
334
-
335
- end
336
-
337
- get '/drinks/searchdrink', to: 'drinks#search_drink'
338
-
339
- resources :drinks, only: [:index,:new,:show,:create,:destroy] do
340
-
341
- collection do
342
-
343
- get 'search'
344
-
345
- end
346
-
347
-
348
-
349
- resources :comments,only: :create
350
-
351
- # あるツイートに対してのコメントと言う
352
-
353
- # 親子関係を表現したパスが、コメント投稿に必要なリクエストの
354
-
355
- # パスになります
356
-
357
-
358
-
359
- resources :trades,only: [:index,:new,:create]
360
-
361
- end
362
-
363
- post '/drinks/:id/trades/new', to: 'trades#create'
364
-
365
- resources :relationships, only: [:create,:destroy]
366
-
367
- post 'like/:drink_id' ,to: 'likes#like', as: 'like'
368
-
369
- delete 'like/:drink_id',to: 'likes#unlike', as: 'unlike'
370
-
371
- end
372
-
373
-
374
-
375
- ```
376
-
377
-
378
-
379
- ### 試したこと
380
-
381
-
382
-
383
- **rails serverのlikes/_like.html.erbで**
384
-
385
-
386
-
387
- ```
388
-
389
- ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007fce4fa14a08>:0x00007fce4fa1c8e8>
390
-
391
- Did you mean? @drink):
392
-
393
- 1: <div class="like" id="like-link-<%= drink.id %>">
394
-
395
- 2: <% if current_user.likes.find_by(drink_id: drin
396
-
397
- ```
398
-
399
-
400
-
401
- とあったので、drinkの部分を@drinkに全部変更したら、
402
-
403
- undefined method `id' for nil:NilClass
404
-
405
- とエラーが出ました
406
-
407
-
408
-
409
- パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
410
-
411
-
412
-
413
- drinkも
414
-
415
- **drinks/index.html.erb**
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
- ```erb
424
-
425
425
  <%= render 'likes/like',locals:{ drink: drink}%>
426
426
 
427
427
  ```