質問編集履歴

1

情報訂正

2020/11/27 04:13

投稿

hurousyotoku500
hurousyotoku500

スコア27

test CHANGED
File without changes
test CHANGED
@@ -10,51 +10,43 @@
10
10
 
11
11
  ### いいねボタンを押した時の挙動
12
12
 
13
- 画面遷移はしており(同じページを再読み込み)、エラーは特に出ていないが、データベースを見ても、いいねが保存されていない状況です。
13
+ 下記のエラー出てい状況です。
14
14
 
15
15
  ### いいねボタンを押した時のログ
16
16
 
17
17
  ```ここに言語を入力
18
18
 
19
- Started GET "/messages/show/%E5%A4%AA%E5%B9%B3%E6%B4%8B%E3%82%AF%E3%83%A9%E3%83%96%E5%A4%A7%E6%B4%97%E3%82%B7%E3%83%A3%E3%83%BC%E3%82%A6%E3%83%83%E3%83%89%E3%82%B3%E3%83%BC%E3%82%B9/18" for ::1 at 2020-11-27 06:55:42 +0900
19
+ Started POST "/likes" for ::1 at 2020-11-27 13:04:30 +0900
20
20
 
21
- Processing by MessagesController#show_message as HTML
21
+ Processing by LikesController#create as HTML
22
22
 
23
- Parameters: {"course"=>"太平洋クラブ大洗シャーウッドコース", "id"=>"18"}
23
+ Parameters: {"authenticity_token"=>"qf1hIs6n7xzMfSiQpK8iFA6tzLn56nzlsLWlkVEP8Ls19Eu9SPJWyNMSUcoBXRFhb2XJaBO42q6FjLi7cqrG3w=="}
24
24
 
25
- Message Load (0.5ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`id` = 18 LIMIT 1
25
+ User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 ORDER BY `users`.`id` ASC LIMIT 1
26
26
 
27
- ↳ app/controllers/messages_controller.rb:19:in `show_message'
27
+ ↳ app/controllers/likes_controller.rb:3:in `create'
28
28
 
29
- Rendering messages/show_message.html.erb within layouts/application
29
+ (0.2ms) BEGIN
30
30
 
31
- User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
31
+ app/controllers/likes_controller.rb:3:in `create'
32
32
 
33
- app/views/messages/show_message.html.erb:2
33
+ Like Exists? (0.4ms) SELECT 1 AS one FROM `likes` WHERE `likes`.`message_id` IS NULL AND `likes`.`user_id` = 4 LIMIT 1
34
34
 
35
- (1.6ms) SELECT COUNT(*) FROM `likes` WHERE `likes`.`message_id` = 18
35
+ app/controllers/likes_controller.rb:3:in `create'
36
36
 
37
- app/views/messages/show_message.html.erb:4
37
+ (0.4ms) ROLLBACK
38
38
 
39
- User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 ORDER BY `users`.`id` ASC LIMIT 1
39
+ app/controllers/likes_controller.rb:3:in `create'
40
40
 
41
- app/views/messages/show_message.html.erb:5
41
+ No template found for LikesController#create, rendering head :no_content
42
42
 
43
- Like Exists? (0.3ms) SELECT 1 AS one FROM `likes` WHERE `likes`.`user_id` = 4 AND `likes`.`message_id` = 18 LIMIT 1
43
+ Completed 204 No Content in 12ms (ActiveRecord: 2.2ms | Allocations: 5543)
44
44
 
45
- ↳ app/models/user.rb:42:in `already_liked?'
46
45
 
47
- User Load (0.7ms) SELECT `users`.* FROM `users` INNER JOIN `likes` ON `users`.`id` = `likes`.`user_id` WHERE `likes`.`message_id` = 18
48
46
 
49
- ↳ app/views/messages/show_message.html.erb:14
50
47
 
51
- Rendered messages/show_message.html.erb within layouts/application (Duration: 10.8ms | Allocations: 4581)
52
48
 
53
- [Webpacker] Everything's up-to-date. Nothing to do
54
49
 
55
- Rendered layouts/_flash.scores.html.erb (Duration: 0.1ms | Allocations: 18)
56
-
57
- Completed 200 OK in 24ms (Views: 16.9ms | ActiveRecord: 4.2ms | Allocations: 9323)
58
50
 
59
51
  ```
60
52
 
@@ -126,6 +118,42 @@
126
118
 
127
119
 
128
120
 
121
+ ```ここに言語を入力
122
+
123
+ #likes_controller.rb
124
+
125
+ class LikesController < ApplicationController
126
+
127
+ def create
128
+
129
+ @like = current_user.likes.create(message_id: params[:message_id])
130
+
131
+ # redirect_to root_path
132
+
133
+ # redirect_back(fallback_location: root_path)
134
+
135
+ end
136
+
137
+
138
+
139
+ def destroy
140
+
141
+ @like = Like.find_by(message_id: params[:message_id], user_id: current_user.id)
142
+
143
+ @like.destroy
144
+
145
+ # redirect_to root_path
146
+
147
+ redirect_back(fallback_location: root_path)
148
+
149
+ end
150
+
151
+ end
152
+
153
+ ```
154
+
155
+
156
+
129
157
  ```ruby
130
158
 
131
159
  #routes.rb