質問編集履歴

2

エラーメッセージを追記しました

2021/12/31 04:48

投稿

iyonegaki
iyonegaki

スコア19

test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,11 @@
28
28
 
29
29
  ```
30
30
 
31
- IPアドレス - - [30/Dec/2021:08:54:20 +0000] "GET /student/answer/result HTTP/1.1" 500 1635 "http://IPアドレス/student/answer" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
31
+ IPアドレス - - [30/Dec/2021:08:53:53 +0000] "GET /student/answer HTTP/1.1" 200 4598 "http://サーバのIPアドレス/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
32
+
33
+ IPアドレス - - [30/Dec/2021:08:54:10 +0000] "POST /student/answer/answer_check HTTP/1.1" 302 119 "http://サーバのIPアドレス/student/answer" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
34
+
35
+ IPアドレス - - [30/Dec/2021:08:54:10 +0000] "GET /student/answer/result HTTP/1.1" 500 1635 "http://サーバのIPアドレス/student/answer" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
32
36
 
33
37
  ```
34
38
 

1

コードを入れた

2021/12/31 04:48

投稿

iyonegaki
iyonegaki

スコア19

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,10 @@
14
14
 
15
15
 
16
16
 
17
+ 理想はこのような画面に遷移したいです,ローカルだと動きます
18
+
19
+ ![イメージ説明](4fbe80638b29985afe19352d3a40befd.png)
20
+
17
21
  デプロイは[このサイト](https://pikawaka.com/rails/ec2_deploy)を参考にしました.
18
22
 
19
23
 
@@ -59,3 +63,373 @@
59
63
  Nginx
60
64
 
61
65
  Unicorn 5.4.1
66
+
67
+
68
+
69
+ ## 追記
70
+
71
+ 汚いですが,このようなコードを書きました.
72
+
73
+ ### コード
74
+
75
+ #### ビュー
76
+
77
+ 解答画面
78
+
79
+ show.html.erb
80
+
81
+ ```
82
+
83
+ <div class="answer-wrapper">
84
+
85
+ <h1>問題解答</h1>
86
+
87
+ <h4 class="answer-label-title">それぞれのユーザーに対する危険度を解答してください</h4>
88
+
89
+ <br>
90
+
91
+ <%= form_with model: @answer, url: student_answer_answer_check_path, local: true do |form| %>
92
+
93
+ <% @virtual_users.each do |virtual_user| %>
94
+
95
+ <p class="answer-label-title"><%= form.label :virtual_user_id, virtual_user.name %>の危険度</p>
96
+
97
+
98
+
99
+ <%= form.label :danger_level, '危険度' %>
100
+
101
+ <%= form.select(:"danger_level[#{virtual_user.id}]", [["現状情報の流出はない", 1], ["今後の投稿次第では危険", 2], ["個人情報が流出している", 3]], {prompt: "危険度を選択してください"}) %>
102
+
103
+ <br>
104
+
105
+ <br>
106
+
107
+ <% end %>
108
+
109
+ <%= form.submit "答え合わせ" %>
110
+
111
+ <% end %>
112
+
113
+ </div>
114
+
115
+
116
+
117
+ ```
118
+
119
+
120
+
121
+ 採点結果の画面
122
+
123
+ result.html.erb
124
+
125
+ ```
126
+
127
+ <div class="answer-wrapper">
128
+
129
+ <h1>採点結果</h1><h4>正解 <%= @num_of_correct %>問/<%= $result.length %>問</h4>
130
+
131
+ <br>
132
+
133
+ <% $result.each do |virtual_user_id, student_ansewer| %>
134
+
135
+ <h4 class="answer-label-title">
136
+
137
+ <%= @answers.find_by(virtual_user_id: virtual_user_id).virtual_user.name %>の危険度
138
+
139
+ </h4>
140
+
141
+ <h5>解答</h5>
142
+
143
+ <span>
144
+
145
+ <% if student_ansewer == 1 %>
146
+
147
+ 現状情報の流出はない
148
+
149
+ <% elsif student_ansewer == 2 %>
150
+
151
+ 今後の投稿次第では危険
152
+
153
+ <% elsif student_ansewer == 3 %>
154
+
155
+ 個人情報が流出している
156
+
157
+ <% else %>
158
+
159
+ 未回答
160
+
161
+ <% end %>
162
+
163
+ </span>
164
+
165
+ <h5>正解</h5>
166
+
167
+ <span>
168
+
169
+ <% if @answers.find_by(virtual_user_id: @answers.find_by(virtual_user_id: virtual_user_id).virtual_user).danger_level == 1 %>
170
+
171
+ 現状情報の流出はない
172
+
173
+ <% elsif @answers.find_by(virtual_user_id: @answers.find_by(virtual_user_id: virtual_user_id).virtual_user).danger_level == 2 %>
174
+
175
+ 今後の投稿次第では危険
176
+
177
+ <% elsif @answers.find_by(virtual_user_id: @answers.find_by(virtual_user_id: virtual_user_id).virtual_user).danger_level == 3 %>
178
+
179
+ 個人情報が流出している
180
+
181
+ <% end %>
182
+
183
+ </span>
184
+
185
+ <div>
186
+
187
+ <h5>解説</h5>
188
+
189
+ <span>解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります解説が入ります</span>
190
+
191
+ </div>
192
+
193
+ <br>
194
+
195
+ <% end %>
196
+
197
+ </div>
198
+
199
+
200
+
201
+ ```
202
+
203
+
204
+
205
+ #### モデル
206
+
207
+ virtual_user.rb
208
+
209
+ ```
210
+
211
+ # == Schema Information
212
+
213
+ #
214
+
215
+ # Table name: virtual_users
216
+
217
+ #
218
+
219
+ # id :integer not null, primary key
220
+
221
+ # catch_copy :text
222
+
223
+ # follow :integer
224
+
225
+ # follower :integer
226
+
227
+ # image :string
228
+
229
+ # name :string
230
+
231
+ # sub_name :string
232
+
233
+ # created_at :datetime not null
234
+
235
+ # updated_at :datetime not null
236
+
237
+ #
238
+
239
+ class VirtualUser < ApplicationRecord
240
+
241
+ # 関連
242
+
243
+ has_many :posts, dependent: :destroy
244
+
245
+ has_many :follows, dependent: :destroy
246
+
247
+ has_many :followings, through: :follows, source: :follower
248
+
249
+ has_many :passive_follows, class_name: 'Follow', foreign_key: 'follower_id', dependent: :destroy
250
+
251
+ has_many :followers, through: :passive_follows, source: :virtual_user
252
+
253
+ has_many :goods
254
+
255
+ has_many :favorites, through: :goods, source: :post
256
+
257
+ has_one :answer, dependent: :destroy
258
+
259
+
260
+
261
+
262
+
263
+ # 委譲
264
+
265
+ def followings_count; followings.count; end
266
+
267
+ def followers_count; followers.count; end
268
+
269
+
270
+
271
+
272
+
273
+ # バリデーション
274
+
275
+ validates :name, presence: true
276
+
277
+ validates :sub_name, presence: true
278
+
279
+ end
280
+
281
+
282
+
283
+ ```
284
+
285
+
286
+
287
+ answer.rb
288
+
289
+ ```
290
+
291
+ # == Schema Information
292
+
293
+ #
294
+
295
+ # Table name: answers
296
+
297
+ #
298
+
299
+ # id :integer not null, primary key
300
+
301
+ # danger_level :integer
302
+
303
+ # created_at :datetime not null
304
+
305
+ # updated_at :datetime not null
306
+
307
+ # virtual_user_id :integer
308
+
309
+ #
310
+
311
+ class Answer < ApplicationRecord
312
+
313
+ # 関連
314
+
315
+ belongs_to :virtual_user
316
+
317
+
318
+
319
+
320
+
321
+ # バリデーション
322
+
323
+ validates :virtual_user_id, uniqueness: true
324
+
325
+ validates :danger_level, presence: true
326
+
327
+ end
328
+
329
+
330
+
331
+ ```
332
+
333
+
334
+
335
+ #### コントローラー
336
+
337
+ ```
338
+
339
+ class Student::AnswersController < ApplicationController
340
+
341
+ before_action :set_virtual_users, only: [:show]
342
+
343
+ before_action :set_answers, only: [:result]
344
+
345
+
346
+
347
+ def show
348
+
349
+ end
350
+
351
+
352
+
353
+ def answer_check
354
+
355
+ $result = []
356
+
357
+ params[:danger_level].each do |key, value|
358
+
359
+ key = key.to_i
360
+
361
+ value = value.to_i
362
+
363
+ set_answer(key)
364
+
365
+ buf = [key, value]
366
+
367
+ if value == @answer.danger_level
368
+
369
+ buf.push(1)
370
+
371
+ $result.push(buf)
372
+
373
+ else
374
+
375
+ buf.push(0)
376
+
377
+ $result.push(buf)
378
+
379
+ end
380
+
381
+ end
382
+
383
+ redirect_to student_answer_result_path
384
+
385
+ end
386
+
387
+
388
+
389
+ def result
390
+
391
+ @num_of_correct = 0
392
+
393
+ $result.each do |result|
394
+
395
+ if result[2] == 1
396
+
397
+ @num_of_correct += 1
398
+
399
+ end
400
+
401
+ end
402
+
403
+ end
404
+
405
+
406
+
407
+ private
408
+
409
+ def set_virtual_users
410
+
411
+ @virtual_users = VirtualUser.all
412
+
413
+ end
414
+
415
+
416
+
417
+ def set_answers
418
+
419
+ @answers = Answer.all
420
+
421
+ end
422
+
423
+
424
+
425
+ def set_answer(answer_id)
426
+
427
+ @answer = Answer.find_by(virtual_user_id: answer_id)
428
+
429
+ end
430
+
431
+ end
432
+
433
+
434
+
435
+ ```