質問編集履歴

2

内容の修正

2021/05/20 16:50

投稿

yuu0000
yuu0000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- ログアウトはdeviseを使用した機能になっております。
17
+ ログアウトはdeviseを使用した機能になっております。また、ログアウトを何も投稿していない時にすると正常にログアウトできるのですが、``投稿後にログアウトをするとエラーが起きる``状況です。
18
18
 
19
19
 
20
20
 
@@ -222,6 +222,154 @@
222
222
 
223
223
 
224
224
 
225
+ **app/controllers/uploads_controller.er**
226
+
227
+
228
+
229
+ ```controller
230
+
231
+ class UploadsController < ApplicationController
232
+
233
+ before_action :authenticate_user!, except: [:index, :show]
234
+
235
+ before_action :set_upload, only: [:show, :edit, :update, :destroy]
236
+
237
+
238
+
239
+
240
+
241
+ def index
242
+
243
+ @uploads = Upload.all.order(created_at: :desc)
244
+
245
+ @tag_list = Tag.all
246
+
247
+ end
248
+
249
+
250
+
251
+ def new
252
+
253
+ @upload_form = UploadForm.new
254
+
255
+ end
256
+
257
+
258
+
259
+ def create
260
+
261
+ @upload_form = UploadForm.new(upload_params)
262
+
263
+ tag_list = params[:upload][:name].split(",")
264
+
265
+ if @upload_form.valid?
266
+
267
+ @upload_form.save(tag_list)
268
+
269
+ redirect_to root_path
270
+
271
+ else
272
+
273
+ render :new
274
+
275
+ end
276
+
277
+ end
278
+
279
+
280
+
281
+ def show
282
+
283
+ @tag = @upload.tags
284
+
285
+ end
286
+
287
+
288
+
289
+ def edit
290
+
291
+ @upload_form = UploadForm.new(upload: @upload)
292
+
293
+ if current_user.id != @upload.user.id
294
+
295
+ redirect_to root_path
296
+
297
+ end
298
+
299
+ end
300
+
301
+
302
+
303
+ def update
304
+
305
+ @upload_form = UploadForm.new(upload_params, upload: @upload)
306
+
307
+ tag_list = params[:upload][:name].split(",")
308
+
309
+ if @upload_form.valid?
310
+
311
+ @upload_form.save(tag_list)
312
+
313
+ redirect_to upload_path(@upload.id)
314
+
315
+ else
316
+
317
+ render :edit
318
+
319
+ end
320
+
321
+ end
322
+
323
+
324
+
325
+ def destroy
326
+
327
+ @upload.image.purge if @upload.image.attached?
328
+
329
+ if current_user.id == @upload.user.id
330
+
331
+ @upload.destroy
332
+
333
+ redirect_to root_path
334
+
335
+ else
336
+
337
+ render :show
338
+
339
+ end
340
+
341
+
342
+
343
+ end
344
+
345
+
346
+
347
+ private
348
+
349
+
350
+
351
+ def upload_params
352
+
353
+ params.require(:upload).permit(:title, :text, :url, :working_day, :day_off, :cafe_wifi_id, :cafe_charging_id, :cafe_smoking_id, :image, :name).merge(user_id: current_user.id)
354
+
355
+ end
356
+
357
+
358
+
359
+ def set_upload
360
+
361
+ @upload = Upload.find(params[:id])
362
+
363
+ end
364
+
365
+ end
366
+
367
+
368
+
369
+ ```
370
+
371
+
372
+
225
373
  **app/views/shared/_header.html.erb**
226
374
 
227
375
 
@@ -292,6 +440,32 @@
292
440
 
293
441
 
294
442
 
443
+ **app/config/routes.rb**
444
+
445
+
446
+
447
+ ```config
448
+
449
+ Rails.application.routes.draw do
450
+
451
+ devise_for :users
452
+
453
+ get 'uploads/index'
454
+
455
+ root to: "uploads#index"
456
+
457
+ resources :uploads
458
+
459
+ resources :users, only: :show
460
+
461
+ end
462
+
463
+
464
+
465
+ ```
466
+
467
+
468
+
295
469
 
296
470
 
297
471
  ### 試したこと
@@ -306,6 +480,54 @@
306
480
 
307
481
 
308
482
 
483
+ ・ログアウトの挙動を調べましたが、投稿をしていない場合ではログアウトができますが、``投稿後にログアウトをするとエラーが起きる``ことがわかりました。
484
+
485
+
486
+
487
+ ・検証としては、``current_userが空``となっているために、ログアウトしようとするとログインユーザーを見つけることができないために起きるエラーと考えました。
488
+
489
+
490
+
491
+ ##エラーメッセージの値の確認
492
+
493
+
494
+
495
+ ```
496
+
497
+ >> @uploads
498
+
499
+ => #<ActiveRecord::Relation [#<Upload id: 1, title: "testcafe", text: "カフェの説明", url: "", working_day: "", day_off: "", cafe_wifi_id: 1, cafe_charging_id: 1, cafe_smoking_id: 1, user_id: 1, created_at: "2021-05-20 16:14:11.950862000 +0000", updated_at: "2021-05-20 16:14:11.997068000 +0000">]>
500
+
501
+
502
+
503
+ >> upload.id
504
+
505
+ => 1
506
+
507
+
508
+
509
+ >> upload
510
+
511
+ => #<Upload id: 1, title: "testcafe", text: "カフェの説明", url: "", working_day: "", day_off: "", cafe_wifi_id: 1, cafe_charging_id: 1, cafe_smoking_id: 1, user_id: 1, created_at: "2021-05-20 16:14:11.950862000 +0000", updated_at: "2021-05-20 16:14:11.997068000 +0000">
512
+
513
+
514
+
515
+ >> current_user
516
+
517
+ => nil
518
+
519
+
520
+
521
+ >> current_user.id
522
+
523
+ NoMethodError: undefined method `id' for nil:NilClass
524
+
525
+ from (eval):1:in `block in _app_views_uploads_index_html_erb___1943206866683830353_21680'
526
+
527
+ ```
528
+
529
+
530
+
309
531
 
310
532
 
311
533
  ### 補足情報(FW/ツールのバージョンなど)

1

エラー文の修正

2021/05/20 16:50

投稿

yuu0000
yuu0000

スコア4

test CHANGED
File without changes
test CHANGED
@@ -28,10 +28,72 @@
28
28
 
29
29
  NoMethodError in Uploads#index
30
30
 
31
+ Showing /Users/fujimotoyuunosuke/RubymineProjects/locat/app/views/uploads/index.html.erb where line #117 raised:
32
+
31
33
 
32
34
 
33
35
  undefined method `id' for nil:NilClass
34
36
 
37
+ Extracted source (around line #117):
38
+
39
+ 115
40
+
41
+ 116
42
+
43
+ 117
44
+
45
+ 118
46
+
47
+ 119
48
+
49
+ 120
50
+
51
+
52
+
53
+ </div>
54
+
55
+ <p class="upload-user">
56
+
57
+ <%= link_to image_tag(upload.user.avatar, class: "avatar-img"), user_path(current_user.id) %>⇦この部分
58
+
59
+ <%= link_to upload.user.name, user_path(current_user.id), class: "upload-user__name" %>
60
+
61
+ </p>
62
+
63
+ </div>
64
+
65
+
66
+
67
+ Extracted source (around line #98):
68
+
69
+ 96
70
+
71
+ 97
72
+
73
+ 98
74
+
75
+ 99
76
+
77
+ 100
78
+
79
+ 101
80
+
81
+
82
+
83
+ <% @uploads.each do |upload| %>
84
+
85
+ <div class="content-page">
86
+
87
+ <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>⇦この部分
88
+
89
+ <div class="upload-contents-wrapper">
90
+
91
+ <div class="upload-img-contents">
92
+
93
+ <%= image_tag upload.image, class: "upload-img" if upload.image.attached? %>
94
+
95
+
96
+
35
97
  Extracted source (around line #96):
36
98
 
37
99
  94
@@ -52,150 +114,90 @@
52
114
 
53
115
  <div id="content-table">
54
116
 
117
+ <% @uploads.each do |upload| %>⇦この部分
118
+
119
+ <div class="content-page">
120
+
121
+ <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>
122
+
123
+ <div
124
+
125
+ ```
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+ ### 該当のソースコード
134
+
135
+ **app/views/uploads/index.html.erb**
136
+
137
+
138
+
139
+ ```erb
140
+
141
+ <div id="content-table">
142
+
55
143
  <% @uploads.each do |upload| %>
56
144
 
57
145
  <div class="content-page">
58
146
 
59
- <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>⇦この部分
147
+ <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>
60
148
 
61
149
  <div class="upload-contents-wrapper">
62
150
 
63
-
64
-
65
- undefined method `id' for nil:NilClass
66
-
67
- Extracted source (around line #98):
68
-
69
- 96
70
-
71
- 97
72
-
73
- 98
74
-
75
- 99
76
-
77
- 100
78
-
79
- 101
80
-
81
-
82
-
83
- <% @uploads.each do |upload| %>
84
-
85
- <div class="content-page">
86
-
87
- <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>⇦この部分
88
-
89
- <div class="upload-contents-wrapper">
90
-
91
151
  <div class="upload-img-contents">
92
152
 
93
153
  <%= image_tag upload.image, class: "upload-img" if upload.image.attached? %>
94
154
 
95
-
155
+ </div>
156
+
96
-
157
+ <div class="upload-contents-detail">
158
+
159
+ <div class="upload-name"><%= upload.title %></div>
160
+
97
- undefined method `id' for nil:NilClass
161
+ <div class="tag-container">
98
-
162
+
99
- Extracted source (around line #96):
163
+ <% upload.tags.each do |tag| %>
164
+
100
-
165
+ <div class="upload-tags">
166
+
167
+ #<%= tag.name %>
168
+
169
+ </div>
170
+
101
- 94
171
+ <% end %>
172
+
102
-
173
+ </div>
174
+
175
+ </div>
176
+
177
+ <div class="upload-contents-explain">
178
+
179
+ <div class="upload-explain"><%= upload.text %></div>
180
+
181
+ </div>
182
+
183
+ <p class="upload-user">
184
+
185
+ <%= link_to image_tag(upload.user.avatar, class: "avatar-img"), user_path(current_user.id) %>
186
+
187
+ <%= link_to upload.user.name, user_path(current_user.id), class: "upload-user__name" %>
188
+
103
- 95
189
+ </p>
190
+
104
-
191
+ </div>
192
+
105
- 96
193
+ <% end %>
194
+
106
-
195
+ </div>
196
+
107
- 97
197
+ <% end %>
108
-
109
- 98
110
-
111
- 99
112
-
113
-
114
198
 
115
199
  </div>
116
200
 
117
- <div id="content-table">
118
-
119
- <% @uploads.each do |upload| %>⇦この部分
120
-
121
- <div class="content-page">
122
-
123
- <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>
124
-
125
- <div class="upload-contents-wrapper">
126
-
127
- ```
128
-
129
-
130
-
131
- ### 該当のソースコード
132
-
133
- **app/views/uploads/index.html.erb**
134
-
135
-
136
-
137
- ```erb
138
-
139
- <div id="content-table">
140
-
141
- <% @uploads.each do |upload| %>
142
-
143
- <div class="content-page">
144
-
145
- <%= link_to upload_path(upload.id), class: "upload-link", method: :get do %>
146
-
147
- <div class="upload-contents-wrapper">
148
-
149
- <div class="upload-img-contents">
150
-
151
- <%= image_tag upload.image, class: "upload-img" if upload.image.attached? %>
152
-
153
- </div>
154
-
155
- <div class="upload-contents-detail">
156
-
157
- <div class="upload-name"><%= upload.title %></div>
158
-
159
- <div class="tag-container">
160
-
161
- <% upload.tags.each do |tag| %>
162
-
163
- <div class="upload-tags">
164
-
165
- #<%= tag.name %>
166
-
167
- </div>
168
-
169
- <% end %>
170
-
171
- </div>
172
-
173
- </div>
174
-
175
- <div class="upload-contents-explain">
176
-
177
- <div class="upload-explain"><%= upload.text %></div>
178
-
179
- </div>
180
-
181
- <p class="upload-user">
182
-
183
- <%= link_to image_tag(upload.user.avatar, class: "avatar-img"), user_path(current_user.id) %>
184
-
185
- <%= link_to upload.user.name, user_path(current_user.id), class: "upload-user__name" %>
186
-
187
- </p>
188
-
189
- </div>
190
-
191
- <% end %>
192
-
193
- </div>
194
-
195
- <% end %>
196
-
197
- </div>
198
-
199
201
  ```
200
202
 
201
203
  **app/controllers/users_controller.rb**