質問編集履歴

4

ログの内容を追記

2021/10/17 08:55

投稿

otyaotya
otyaotya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -22,207 +22,321 @@
22
22
 
23
23
  ```
24
24
 
25
+ Started DELETE "/posts/1/likes/1" for 172.18.0.1 at 2021-10-17 08:46:58 +0000
26
+
25
- Completed 500 Internal Server Error in 36ms (ActiveRecord: 10.1ms)
27
+ Completed 500 Internal Server Error in 101ms (ActiveRecord: 2.5ms)
28
+
29
+
30
+
31
+
32
+
26
-
33
+ Completed 500 Internal Server Error in 177ms (ActiveRecord: 4.9ms)
34
+
35
+
36
+
37
+
38
+
27
-
39
+ Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
40
+
28
-
41
+ Processing by LikesController#destroy as JS
42
+
43
+
44
+
29
-
45
+ Rendering posts/show.html.erb within layouts/application
46
+
47
+
48
+
49
+ NoMethodError (undefined method `post' for nil:NilClass):
50
+
51
+ NoMethodError (undefined method `post' for nil:NilClass):
52
+
53
+ Like Load (1.3ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 1 AND `likes`.`post_id` = 1 LIMIT 1
54
+
55
+ Parameters: {"post_id"=>"1", "id"=>"1"}
56
+
57
+
58
+
59
+ User Load (5.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
60
+
61
+ Rendered posts/show.html.erb within layouts/application (22.7ms)
62
+
63
+
64
+
65
+ app/controllers/likes_controller.rb:14:in `destroy'
66
+
67
+ app/controllers/likes_controller.rb:14:in `destroy'
68
+
69
+ Like Load (1.7ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`id` = 1 LIMIT 1
70
+
71
+ Completed 500 Internal Server Error in 254ms (ActiveRecord: 6.9ms)
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ NoMethodError (undefined method `post' for nil:NilClass):
80
+
81
+
82
+
83
+ app/controllers/likes_controller.rb:14:in `destroy'
84
+
85
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
86
+
87
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
88
+
89
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (2.1ms)
30
90
 
31
91
  Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
32
92
 
93
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
94
+
95
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.1ms)
96
+
97
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
98
+
99
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
100
+
101
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
102
+
103
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
104
+
105
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (358.9ms)
106
+
107
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
108
+
109
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (1.0ms)
110
+
111
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
112
+
113
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (6.6ms)
114
+
115
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.8ms)
116
+
117
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
118
+
119
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
120
+
121
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.7ms)
122
+
123
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.3ms)
124
+
125
+ Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
126
+
127
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (305.1ms)
128
+
129
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (2.2ms)
130
+
131
+ Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (261.0ms)
132
+
133
+ Completed 200 OK in 2667ms (Views: 2524.5ms | ActiveRecord: 4.6ms)
134
+
135
+
136
+
137
+
138
+
139
+ ```
140
+
141
+ となっていましたのでdestroyを確認しました。
142
+
143
+
144
+
145
+ ```
146
+
147
+ def destroy
148
+
149
+ @like = Like.find_by(id: params[:id])
150
+
151
+ @post = @like.post
152
+
153
+ if @like.destroy
154
+
155
+ respond_to :js
156
+
157
+ end
158
+
159
+ end
160
+
161
+ ```
162
+
163
+
164
+
165
+ 特に打ち間違いなどもありませんでした。
166
+
167
+
168
+
169
+
170
+
171
+ またNoMethodError (undefined method post' for nil:NilClass): というところから
172
+
173
+ エラーを探そうとコンソールで調べましたが下記の通りに表示されました。
174
+
175
+ ```
176
+
177
+ @like = Like.find_by(id: params[:id]) NameError: undefined local variable or methodparams' for main:Object
178
+
179
+ ```
180
+
181
+ 調べましたがこれはhttpリクエストが発行されてからparamsが発生するためこのエラーは問題ないように思います。
182
+
183
+
184
+
185
+ destroy周辺でエラーになっているとは思うのですが
186
+
187
+ これ以降どのようにしたらいいかわからず質問させていただきました。
188
+
189
+ ※リロードするといいねに切り替わります。
190
+
191
+
192
+
193
+ お手数ですが教えていただけましたら助かります。
194
+
195
+ 何卒宜しくお願い申し上げます。
196
+
197
+
198
+
199
+ show.html.erb
200
+
201
+ ```
202
+
203
+ <div id="like-icon-post-<%= @post.id.to_s %>">
204
+
205
+ <% if @post.liked_by(current_user).present? %>
206
+
207
+ <%= link_to "いいねを取り消す",post_like_path(@post.id,@post.liked_by(current_user)),method: :DELETE, remote: true, class: "loved hide-text"%>
208
+
209
+ <% else %>
210
+
211
+ <%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true, class: "love hide-text"%>
212
+
213
+ <%end%>
214
+
215
+ </div>
216
+
217
+ ```
218
+
219
+ destroy.js.erb
220
+
221
+ ```
222
+
223
+ $('#like-icon-post-<%= @post.id.to_s%>').
224
+
225
+ html('<%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true,class: "love hide-text" %>');
226
+
227
+
228
+
229
+ ```
230
+
231
+
232
+
233
+ create.js.erb
234
+
235
+
236
+
237
+ ```
238
+
239
+ $('#like-icon-post-<%= @post.id.to_s %>').
240
+
241
+ html('<%= link_to "いいねを取り消す", post_like_path(@post.id, @like), method: :DELETE, remote: true, class: "loved hide-text" %>');
242
+
243
+ ```
244
+
245
+
246
+
247
+ LikesController
248
+
249
+ ```
250
+
251
+ def create
252
+
253
+ @like = current_user.likes.build(like_params)
254
+
255
+ @post = @like.post
256
+
257
+ if @like.save
258
+
259
+ respond_to :js
260
+
261
+ end
262
+
263
+ end
264
+
265
+ ```
266
+
267
+
268
+
269
+ routes.rb
270
+
271
+ ```
272
+
273
+ Rails.application.routes.draw do
274
+
275
+
276
+
277
+ root to: 'home#top'
278
+
279
+ devise_for :users
280
+
281
+
282
+
283
+ resources :users
284
+
33
285
 
34
286
 
35
- Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
36
-
37
- NoMethodError (undefined method `post' for nil:NilClass):
38
-
39
-
40
-
41
- app/controllers/likes_controller.rb:14:in `destroy'
42
-
43
-
44
-
45
- ```
46
-
47
- となっていましたのでdestroyを確認しました。
48
-
49
-
50
-
51
- ```
52
-
53
- def destroy
54
-
55
- @like = Like.find_by(id: params[:id])
56
-
57
- @post = @like.post
58
-
59
- if @like.destroy
60
-
61
- respond_to :js
287
+ resources :posts do
288
+
62
-
289
+ resources :likes, only: [:create, :destroy]
290
+
63
- end
291
+ resources :comments
64
292
 
65
293
  end
66
294
 
295
+
296
+
297
+ end
298
+
67
- ```
299
+ ```
68
-
69
-
70
-
300
+
301
+
302
+
71
- 特に打ち間違いなどもありませんでした。
303
+ post.rb
72
-
73
-
74
-
75
-
76
-
77
- またNoMethodError (undefined method post' for nil:NilClass): というところから
304
+
78
-
79
- エラーを探そうとコンソールで調べましたが下記の通りに表示されました。
80
-
81
- ```
305
+ ```
82
-
83
- @like = Like.find_by(id: params[:id]) NameError: undefined local variable or methodparams' for main:Object
306
+
84
-
85
- ```
86
-
87
- 調べましたがこれはhttpリクエストが発行されてからparamsが発生するためこのエラーは問題ないように思います。
88
-
89
-
90
-
91
- destroy周辺でエラーになっているとは思うのですが
92
-
93
- これ以降どのようにしたらいいかわからず質問させていただきました。
94
-
95
- ※リロードするといいねに切り替わります。
96
-
97
-
98
-
99
- お手数ですが教えていただけましたら助かります。
100
-
101
- 何卒宜しくお願い申し上げます。
102
-
103
-
104
-
105
- show.html.erb
106
-
107
- ```
108
-
109
- <div id="like-icon-post-<%= @post.id.to_s %>">
110
-
111
- <% if @post.liked_by(current_user).present? %>
112
-
113
- <%= link_to "いいねを取り消す",post_like_path(@post.id,@post.liked_by(current_user)),method: :DELETE, remote: true, class: "loved hide-text"%>
114
-
115
- <% else %>
116
-
117
- <%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true, class: "love hide-text"%>
118
-
119
- <%end%>
120
-
121
- </div>
122
-
123
- ```
124
-
125
- destroy.js.erb
126
-
127
- ```
128
-
129
- $('#like-icon-post-<%= @post.id.to_s%>').
130
-
131
- html('<%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true,class: "love hide-text" %>');
132
-
133
-
134
-
135
- ```
136
-
137
-
138
-
139
- create.js.erb
140
-
141
-
142
-
143
- ```
144
-
145
- $('#like-icon-post-<%= @post.id.to_s %>').
146
-
147
- html('<%= link_to "いいねを取り消す", post_like_path(@post.id, @like), method: :DELETE, remote: true, class: "loved hide-text" %>');
148
-
149
- ```
150
-
151
-
152
-
153
- LikesController
154
-
155
- ```
156
-
157
- def create
158
-
159
- @like = current_user.likes.build(like_params)
307
+ class Post < ApplicationRecord
160
-
161
- @post = @like.post
308
+
162
-
163
- if @like.save
164
-
165
- respond_to :js
309
+ belongs_to :user
310
+
166
-
311
+ has_many :likes,dependent: :destroy
312
+
313
+
314
+
167
- end
315
+ def liked_by(user)
316
+
317
+ Like.find_by(user_id: user.id, post_id: id)
168
318
 
169
319
  end
170
320
 
171
- ```
172
-
173
-
174
-
175
- routes.rb
176
-
177
- ```
178
-
179
- Rails.application.routes.draw do
180
-
181
-
182
-
183
- root to: 'home#top'
184
-
185
- devise_for :users
186
-
187
-
188
-
189
- resources :users
190
-
191
-
192
-
193
- resources :posts do
194
-
195
- resources :likes, only: [:create, :destroy]
196
-
197
- resources :comments
198
-
199
- end
200
-
201
-
202
-
203
321
  end
204
322
 
323
+
324
+
205
- ```
325
+ ```
206
-
207
-
208
-
326
+
209
- post.rb
327
+ like.rb
210
-
328
+
211
- ```
329
+ ```
212
-
330
+
213
- class Post < ApplicationRecord
331
+ class Like < ApplicationRecord
214
332
 
215
333
  belongs_to :user
216
334
 
217
- has_many :likes,dependent: :destroy
218
-
219
-
220
-
221
- def liked_by(user)
335
+ belongs_to :post
222
-
336
+
337
+
338
+
223
- Like.find_by(user_id: user.id, post_id: id)
339
+ validates :user_id, uniqueness: { scope: :post_id }
224
-
225
- end
226
340
 
227
341
  end
228
342
 
@@ -230,26 +344,6 @@
230
344
 
231
345
  ```
232
346
 
233
- like.rb
234
-
235
- ```
236
-
237
- class Like < ApplicationRecord
238
-
239
- belongs_to :user
240
-
241
- belongs_to :post
242
-
243
-
244
-
245
- validates :user_id, uniqueness: { scope: :post_id }
246
-
247
- end
248
-
249
-
250
-
251
- ```
252
-
253
347
 
254
348
 
255
349
  環境は以下の通りです。

3

create.js.erb,LikesControllerのdef createを追加

2021/10/17 08:55

投稿

otyaotya
otyaotya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -46,6 +46,8 @@
46
46
 
47
47
  となっていましたのでdestroyを確認しました。
48
48
 
49
+
50
+
49
51
  ```
50
52
 
51
53
  def destroy
@@ -64,6 +66,8 @@
64
66
 
65
67
  ```
66
68
 
69
+
70
+
67
71
  特に打ち間違いなどもありませんでした。
68
72
 
69
73
 
@@ -126,15 +130,43 @@
126
130
 
127
131
  html('<%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true,class: "love hide-text" %>');
128
132
 
133
+
134
+
135
+ ```
136
+
137
+
138
+
129
139
  create.js.erb
130
140
 
131
141
 
132
142
 
143
+ ```
144
+
133
145
  $('#like-icon-post-<%= @post.id.to_s %>').
134
146
 
135
- html('<%= link_to "いいねを取り消す",post_like_path(@post.id,@like),method: :DELETE,remote: true,class: "loved hide-text"%>');
147
+ html('<%= link_to "いいねを取り消す", post_like_path(@post.id, @like), method: :DELETE, remote: true, class: "loved hide-text" %>');
148
+
136
-
149
+ ```
150
+
151
+
152
+
137
-
153
+ LikesController
154
+
155
+ ```
156
+
157
+ def create
158
+
159
+ @like = current_user.likes.build(like_params)
160
+
161
+ @post = @like.post
162
+
163
+ if @like.save
164
+
165
+ respond_to :js
166
+
167
+ end
168
+
169
+ end
138
170
 
139
171
  ```
140
172
 

2

誤字修正

2021/10/07 23:17

投稿

otyaotya
otyaotya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -80,7 +80,7 @@
80
80
 
81
81
  ```
82
82
 
83
- 調べましたがこれはhttpリクエストが発行されているからparamsが存在するためこのエラーは問題ないように思います。
83
+ 調べましたがこれはhttpリクエストが発行されてからparamsが発生するためこのエラーは問題ないように思います。
84
84
 
85
85
 
86
86
 

1

タグの追加

2021/10/06 08:27

投稿

otyaotya
otyaotya

スコア1

test CHANGED
File without changes
test CHANGED
File without changes