質問編集履歴

5

追記

2020/06/06 12:02

投稿

akaki
akaki

スコア15

test CHANGED
@@ -1 +1 @@
1
- 【Rails×Ajax】いいね機能を実装たいが上手くいかない
1
+ 【Rails×Ajax】なぜかいいねボタンのPOSTだけ反応しない
test CHANGED
@@ -8,9 +8,9 @@
8
8
 
9
9
  上記のQiitaを参考に実装しており、記事通りにコードを書きましたが適切に処理されません。
10
10
 
11
- 具体的には、いいねボタンを押ても全反応なくリロードを押してもずっといいねボタンが反応なくなるいった状況です。
11
+ 具体的には、いいねの取り消にはきちんとAjaxが効のですが、いいねようしても
12
12
 
13
- Likesコントローラーの@cafeteria = Cafeteria.find(params[:id])」の記述を削除れば、いねボタンを押すびにリロードは必要ですがいねがきるようになります。
13
+ 500 (Internal Server Error)」になりま。ご教授いただけると幸いです。
14
14
 
15
15
 
16
16
 
@@ -24,23 +24,29 @@
24
24
 
25
25
  いいねボタンを押しても反応がなく、リロードしなければいいねが増えないといった状況
26
26
 
27
- [エラー画](https://i.gyazo.com/2798df89ecc1c650edf86a409fe5c376.png)
27
+ [エラー画](https://i.gyazo.com/706565883715fd81882a42db9a4919de.png)
28
28
 
29
29
  ```
30
30
 
31
- Processing by LikesController#create as JS
31
+ ActionView::Template::Error (No route matches {:action=>"destroy", :cafeteria_id=>#<Cafeteria id: 1, name: "ひだまり食堂", detail: "たのしい!", addres: "練馬区", user_id: 1, created_at: "2020-05-27 14:40:41", updated_at: "2020-05-27 14:40:41", image: "child.jpg">, :controller=>"likes"}, missing required keys: [:id]):
32
32
 
33
- Parameters: {"cafeteria_id"=>"1"}
33
+ 1: - if user_signed_in?
34
34
 
35
- Completed 404 Not Found in 5ms (ActiveRecord: 0.0ms)
35
+ 2: - if current_user.already_liked?(@cafeteria)
36
+
37
+ 3: = link_to cafeteria_like_path(@cafeteria), method: :delete, remote: true do
38
+
39
+ 4: %i.fas.fa-heart.like-btn
40
+
41
+ 5: = @cafeteria.likes.count
42
+
43
+ 6: - else
36
44
 
37
45
 
38
46
 
39
- ActiveRecord::RecordNotFound (Couldn't find Cafeteria without an ID):
47
+ app/views/likes/_like.html.haml:3:in `_app_views_likes__like_html_haml___3068607622047569957_70180034584120'
40
48
 
41
-
42
-
43
- app/controllers/likes_controller.rb:4:in `create'
49
+ app/views/likes/create.js.haml:1:in `_app_views_likes_create_js_haml__514421159198398937_70180035075840'
44
50
 
45
51
  ```
46
52
 
@@ -54,7 +60,7 @@
54
60
 
55
61
  def create
56
62
 
57
- @cafeteria = Cafeteria.find(params[:id])
63
+ @cafeteria = Cafeteria.find(params[:cafeteria_id])
58
64
 
59
65
  @like = current_user.likes.create(cafeteria_id: params[:cafeteria_id])
60
66
 
@@ -66,7 +72,7 @@
66
72
 
67
73
  def destroy
68
74
 
69
- @cafeteria = Cafeteria.find(params[:id])
75
+ @cafeteria = Cafeteria.find(params[:cafeteria_id])
70
76
 
71
77
  @like = Like.find_by(cafeteria_id: params[:cafeteria_id], user_id: current_user.id)
72
78
 

4

追記

2020/06/06 12:02

投稿

akaki
akaki

スコア15

test CHANGED
File without changes
test CHANGED
@@ -25,6 +25,24 @@
25
25
  いいねボタンを押しても反応がなく、リロードしなければいいねが増えないといった状況
26
26
 
27
27
  [エラー画像](https://i.gyazo.com/2798df89ecc1c650edf86a409fe5c376.png)
28
+
29
+ ```
30
+
31
+ Processing by LikesController#create as JS
32
+
33
+ Parameters: {"cafeteria_id"=>"1"}
34
+
35
+ Completed 404 Not Found in 5ms (ActiveRecord: 0.0ms)
36
+
37
+
38
+
39
+ ActiveRecord::RecordNotFound (Couldn't find Cafeteria without an ID):
40
+
41
+
42
+
43
+ app/controllers/likes_controller.rb:4:in `create'
44
+
45
+ ```
28
46
 
29
47
 
30
48
 

3

追記

2020/06/06 11:45

投稿

akaki
akaki

スコア15

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,7 @@
24
24
 
25
25
  いいねボタンを押しても反応がなく、リロードしなければいいねが増えないといった状況
26
26
 
27
- [エラー画像](https://i.gyazo.com/f901ec39de3d201a6361b2c902d8fb89.png)
27
+ [エラー画像](https://i.gyazo.com/2798df89ecc1c650edf86a409fe5c376.png)
28
-
29
-
30
28
 
31
29
 
32
30
 

2

追記

2020/06/06 11:40

投稿

akaki
akaki

スコア15

test CHANGED
File without changes
test CHANGED
@@ -23,6 +23,10 @@
23
23
 
24
24
 
25
25
  いいねボタンを押しても反応がなく、リロードしなければいいねが増えないといった状況
26
+
27
+ [エラー画像](https://i.gyazo.com/f901ec39de3d201a6361b2c902d8fb89.png)
28
+
29
+
26
30
 
27
31
 
28
32
 

1

修正

2020/06/06 11:28

投稿

akaki
akaki

スコア15

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  いいね機能をAjaxで実装しようとしています。
6
6
 
7
- https://qiita.com/naberina/items/c6b5c8d7756cb882fb20
7
+ [参考記事](https://qiita.com/naberina/items/c6b5c8d7756cb882fb20)
8
8
 
9
9
  上記のQiitaを参考に実装しており、記事通りにコードを書きましたが適切に処理されません。
10
10
 
@@ -120,7 +120,7 @@
120
120
 
121
121
 
122
122
 
123
- https://qiita.com/naberina/items/c6b5c8d7756cb882fb20
123
+ [参考記事](https://qiita.com/naberina/items/c6b5c8d7756cb882fb20)
124
124
 
125
125
  この記事の通りに進めましたが上手くいきませんでした。
126
126