質問編集履歴
4
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -231,3 +231,141 @@
|
|
231
231
|
|
232
232
|
|
233
233
|
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
追記コード3
|
238
|
+
|
239
|
+
```problemscontroller
|
240
|
+
|
241
|
+
class ProblemsController < ApplicationController
|
242
|
+
|
243
|
+
before_action :logged_in_user
|
244
|
+
|
245
|
+
before_action :correct_user, only: %i[edit update]
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
def show
|
250
|
+
|
251
|
+
@problem = Problem.find(params[:id])
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
def new
|
258
|
+
|
259
|
+
@problem = Problem.new
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def create
|
266
|
+
|
267
|
+
@problem = current_user.problems.build(problem_params)
|
268
|
+
|
269
|
+
@problem.picture.attach(params[:problem][:picture])
|
270
|
+
|
271
|
+
if @problem.save
|
272
|
+
|
273
|
+
flash[:success] = '問題が作成されました!'
|
274
|
+
|
275
|
+
redirect_to problem_path(@problem)
|
276
|
+
|
277
|
+
else
|
278
|
+
|
279
|
+
render 'problems/new'
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
def edit
|
288
|
+
|
289
|
+
@problem = Problem.find(params[:id])
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
def update
|
296
|
+
|
297
|
+
@problem = Problem.find(params[:id])
|
298
|
+
|
299
|
+
if @problem.update(problem_params)
|
300
|
+
|
301
|
+
flash[:succcess] = '問題情報が更新されました!'
|
302
|
+
|
303
|
+
redirect_to @problem
|
304
|
+
|
305
|
+
else
|
306
|
+
|
307
|
+
render 'edit'
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def destroy
|
316
|
+
|
317
|
+
@problem = Problem.find(params[:id])
|
318
|
+
|
319
|
+
if current_user.admin? || current_user?(@problem.user)
|
320
|
+
|
321
|
+
@problem.destroy
|
322
|
+
|
323
|
+
flash[:success] = '問題が削除されました'
|
324
|
+
|
325
|
+
redirect_to request.referer == user_url(@problem.user) ? user_url(@problem.user) : root_url
|
326
|
+
|
327
|
+
else
|
328
|
+
|
329
|
+
flash[:danger] = '別アカウントの問題は削除できません'
|
330
|
+
|
331
|
+
redirect_to root_url
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
def rand
|
338
|
+
|
339
|
+
@problem = Problem.order("RAND()").take
|
340
|
+
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
private
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
def problem_params
|
352
|
+
|
353
|
+
params.require(:problem).permit(:study_type, :title, :explanation_text, :problem_text, :answer, :problem_explanation, :taget_age, :reference, :picture)
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
def correct_user
|
360
|
+
|
361
|
+
# 現在のユーザーが更新対象の問題を保有しているかどうか確認
|
362
|
+
|
363
|
+
@problem = current_user.problems.find_by(id: params[:id])
|
364
|
+
|
365
|
+
redirect_to root_url if @problem.nil?
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
```
|
3
変更コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -143,3 +143,91 @@
|
|
143
143
|
|
144
144
|
|
145
145
|
```
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
変更したコード
|
150
|
+
|
151
|
+
```rubycontroller
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class QuestionController < ApplicationController
|
156
|
+
|
157
|
+
before_action :logged_in_user
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
def show
|
162
|
+
|
163
|
+
@question = Problem.find_by(params[:id])⇦追加しました
|
164
|
+
|
165
|
+
@problems = Problem.offset(rand(Problem.count)).limit(1).pluck(:study_type, :explanation_text, :problem_text).first ⇦ここが変わっています
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def update ↓ここが変わっています
|
172
|
+
|
173
|
+
@question = Problem.find_by(params[:id])
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
if @question.answer == params[:problem][:answer]
|
178
|
+
|
179
|
+
flash[:notice] = "大正解"
|
180
|
+
|
181
|
+
else
|
182
|
+
|
183
|
+
flash[:notice] = "はずれ"
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
render :show
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
```rubyview
|
200
|
+
|
201
|
+
<% provide(:title, "問題出題") %>
|
202
|
+
|
203
|
+
<div class="container">
|
204
|
+
|
205
|
+
<div class="row">
|
206
|
+
|
207
|
+
<%= form_with model: @question, url: {contller: 'question', action: 'update'} do |f| %>
|
208
|
+
|
209
|
+
<h1>問題</h1>
|
210
|
+
|
211
|
+
<ul class="problems">
|
212
|
+
|
213
|
+
ここが変わっています⇨ 教科:<%= raw(@problems.map{|i| h(i)}.join("<br><br>")) %> </ul>
|
214
|
+
|
215
|
+
<div class="ans">
|
216
|
+
|
217
|
+
<h2>答え</h2>
|
218
|
+
|
219
|
+
<%= f.text_field :answer%>
|
220
|
+
|
221
|
+
</div>
|
222
|
+
|
223
|
+
<%= f.submit "回答", class: "btn btn-primary" %>
|
224
|
+
|
225
|
+
<% end %>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
</div>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
```
|
2
コード再追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -125,3 +125,21 @@
|
|
125
125
|
question GET /question(.:format) question#show
|
126
126
|
|
127
127
|
PATCH /question(.:format) question#update
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
再追記コード
|
132
|
+
|
133
|
+
```ここに言語を入力
|
134
|
+
|
135
|
+
ruby.controller
|
136
|
+
|
137
|
+
def show
|
138
|
+
|
139
|
+
@problems = Problem.offset( rand(Problem.count)).limit(1).pluck(:study_type, :explanation_text, :problem_text).join("<br>")
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```
|
1
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -33,3 +33,95 @@
|
|
33
33
|
ruby 3.0.2
|
34
34
|
|
35
35
|
macOS 11.6
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
追記コード
|
42
|
+
|
43
|
+
```ここに言語を入力
|
44
|
+
|
45
|
+
ruby.controller
|
46
|
+
|
47
|
+
def update
|
48
|
+
|
49
|
+
@question = Problem.find(params[:id])
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
if @question.answer == params[:problem][:answer]
|
54
|
+
|
55
|
+
flash[:notice] = "大正解"
|
56
|
+
|
57
|
+
else
|
58
|
+
|
59
|
+
flash[:notice] = "はずれ"
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
render :show
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
```ここに言語を入力
|
72
|
+
|
73
|
+
ruby.show.html
|
74
|
+
|
75
|
+
<% provide(:title, "問題出題") %>
|
76
|
+
|
77
|
+
<div class="container">
|
78
|
+
|
79
|
+
<div class="row">
|
80
|
+
|
81
|
+
<%= form_with model: @question, url: {contller: 'question', action: 'update'} do |f| %>
|
82
|
+
|
83
|
+
<h1>問題</h1>
|
84
|
+
|
85
|
+
<ul class="problems">
|
86
|
+
|
87
|
+
<%= @problems.html_safe %>
|
88
|
+
|
89
|
+
</ul>
|
90
|
+
|
91
|
+
<div class="ans">
|
92
|
+
|
93
|
+
<h2>答え</h2>
|
94
|
+
|
95
|
+
< %= f.text_field :answer%>
|
96
|
+
|
97
|
+
</div>
|
98
|
+
|
99
|
+
<%= f.submit "回答", class: "btn btn-primary" %>
|
100
|
+
|
101
|
+
<% end %>
|
102
|
+
|
103
|
+
</div>
|
104
|
+
|
105
|
+
</div>
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
```ここに言語を入力
|
112
|
+
|
113
|
+
routes
|
114
|
+
|
115
|
+
get '/question', to: 'question#show'
|
116
|
+
|
117
|
+
patch '/question', to: 'question#update'
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
ログ
|
124
|
+
|
125
|
+
question GET /question(.:format) question#show
|
126
|
+
|
127
|
+
PATCH /question(.:format) question#update
|