質問編集履歴
4
コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -114,4 +114,73 @@
|
|
114
114
|
</div>
|
115
115
|
</div>
|
116
116
|
|
117
|
+
```
|
118
|
+
|
119
|
+
追記コード3
|
120
|
+
```problemscontroller
|
121
|
+
class ProblemsController < ApplicationController
|
122
|
+
before_action :logged_in_user
|
123
|
+
before_action :correct_user, only: %i[edit update]
|
124
|
+
|
125
|
+
def show
|
126
|
+
@problem = Problem.find(params[:id])
|
127
|
+
end
|
128
|
+
|
129
|
+
def new
|
130
|
+
@problem = Problem.new
|
131
|
+
end
|
132
|
+
|
133
|
+
def create
|
134
|
+
@problem = current_user.problems.build(problem_params)
|
135
|
+
@problem.picture.attach(params[:problem][:picture])
|
136
|
+
if @problem.save
|
137
|
+
flash[:success] = '問題が作成されました!'
|
138
|
+
redirect_to problem_path(@problem)
|
139
|
+
else
|
140
|
+
render 'problems/new'
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def edit
|
145
|
+
@problem = Problem.find(params[:id])
|
146
|
+
end
|
147
|
+
|
148
|
+
def update
|
149
|
+
@problem = Problem.find(params[:id])
|
150
|
+
if @problem.update(problem_params)
|
151
|
+
flash[:succcess] = '問題情報が更新されました!'
|
152
|
+
redirect_to @problem
|
153
|
+
else
|
154
|
+
render 'edit'
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def destroy
|
159
|
+
@problem = Problem.find(params[:id])
|
160
|
+
if current_user.admin? || current_user?(@problem.user)
|
161
|
+
@problem.destroy
|
162
|
+
flash[:success] = '問題が削除されました'
|
163
|
+
redirect_to request.referer == user_url(@problem.user) ? user_url(@problem.user) : root_url
|
164
|
+
else
|
165
|
+
flash[:danger] = '別アカウントの問題は削除できません'
|
166
|
+
redirect_to root_url
|
167
|
+
end
|
168
|
+
|
169
|
+
def rand
|
170
|
+
@problem = Problem.order("RAND()").take
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
def problem_params
|
177
|
+
params.require(:problem).permit(:study_type, :title, :explanation_text, :problem_text, :answer, :problem_explanation, :taget_age, :reference, :picture)
|
178
|
+
end
|
179
|
+
|
180
|
+
def correct_user
|
181
|
+
# 現在のユーザーが更新対象の問題を保有しているかどうか確認
|
182
|
+
@problem = current_user.problems.find_by(id: params[:id])
|
183
|
+
redirect_to root_url if @problem.nil?
|
184
|
+
end
|
185
|
+
end
|
117
186
|
```
|
3
変更コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -70,4 +70,48 @@
|
|
70
70
|
@problems = Problem.offset( rand(Problem.count)).limit(1).pluck(:study_type, :explanation_text, :problem_text).join("<br>")
|
71
71
|
end
|
72
72
|
|
73
|
+
```
|
74
|
+
|
75
|
+
変更したコード
|
76
|
+
```rubycontroller
|
77
|
+
|
78
|
+
class QuestionController < ApplicationController
|
79
|
+
before_action :logged_in_user
|
80
|
+
|
81
|
+
def show
|
82
|
+
@question = Problem.find_by(params[:id])⇦追加しました
|
83
|
+
@problems = Problem.offset(rand(Problem.count)).limit(1).pluck(:study_type, :explanation_text, :problem_text).first ⇦ここが変わっています
|
84
|
+
end
|
85
|
+
|
86
|
+
def update ↓ここが変わっています
|
87
|
+
@question = Problem.find_by(params[:id])
|
88
|
+
|
89
|
+
if @question.answer == params[:problem][:answer]
|
90
|
+
flash[:notice] = "大正解"
|
91
|
+
else
|
92
|
+
flash[:notice] = "はずれ"
|
93
|
+
end
|
94
|
+
|
95
|
+
render :show
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
```rubyview
|
101
|
+
<% provide(:title, "問題出題") %>
|
102
|
+
<div class="container">
|
103
|
+
<div class="row">
|
104
|
+
<%= form_with model: @question, url: {contller: 'question', action: 'update'} do |f| %>
|
105
|
+
<h1>問題</h1>
|
106
|
+
<ul class="problems">
|
107
|
+
ここが変わっています⇨ 教科:<%= raw(@problems.map{|i| h(i)}.join("<br><br>")) %> </ul>
|
108
|
+
<div class="ans">
|
109
|
+
<h2>答え</h2>
|
110
|
+
<%= f.text_field :answer%>
|
111
|
+
</div>
|
112
|
+
<%= f.submit "回答", class: "btn btn-primary" %>
|
113
|
+
<% end %>
|
114
|
+
</div>
|
115
|
+
</div>
|
116
|
+
|
73
117
|
```
|
2
コード再追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -61,4 +61,13 @@
|
|
61
61
|
|
62
62
|
ログ
|
63
63
|
question GET /question(.:format) question#show
|
64
|
-
PATCH /question(.:format) question#update
|
64
|
+
PATCH /question(.:format) question#update
|
65
|
+
|
66
|
+
再追記コード
|
67
|
+
```ここに言語を入力
|
68
|
+
ruby.controller
|
69
|
+
def show
|
70
|
+
@problems = Problem.offset( rand(Problem.count)).limit(1).pluck(:study_type, :explanation_text, :problem_text).join("<br>")
|
71
|
+
end
|
72
|
+
|
73
|
+
```
|
1
コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,4 +15,50 @@
|
|
15
15
|
補足データ
|
16
16
|
rails 6.14
|
17
17
|
ruby 3.0.2
|
18
|
-
macOS 11.6
|
18
|
+
macOS 11.6
|
19
|
+
|
20
|
+
|
21
|
+
追記コード
|
22
|
+
```ここに言語を入力
|
23
|
+
ruby.controller
|
24
|
+
def update
|
25
|
+
@question = Problem.find(params[:id])
|
26
|
+
|
27
|
+
if @question.answer == params[:problem][:answer]
|
28
|
+
flash[:notice] = "大正解"
|
29
|
+
else
|
30
|
+
flash[:notice] = "はずれ"
|
31
|
+
end
|
32
|
+
|
33
|
+
render :show
|
34
|
+
end
|
35
|
+
```
|
36
|
+
```ここに言語を入力
|
37
|
+
ruby.show.html
|
38
|
+
<% provide(:title, "問題出題") %>
|
39
|
+
<div class="container">
|
40
|
+
<div class="row">
|
41
|
+
<%= form_with model: @question, url: {contller: 'question', action: 'update'} do |f| %>
|
42
|
+
<h1>問題</h1>
|
43
|
+
<ul class="problems">
|
44
|
+
<%= @problems.html_safe %>
|
45
|
+
</ul>
|
46
|
+
<div class="ans">
|
47
|
+
<h2>答え</h2>
|
48
|
+
< %= f.text_field :answer%>
|
49
|
+
</div>
|
50
|
+
<%= f.submit "回答", class: "btn btn-primary" %>
|
51
|
+
<% end %>
|
52
|
+
</div>
|
53
|
+
</div>
|
54
|
+
```
|
55
|
+
|
56
|
+
```ここに言語を入力
|
57
|
+
routes
|
58
|
+
get '/question', to: 'question#show'
|
59
|
+
patch '/question', to: 'question#update'
|
60
|
+
```
|
61
|
+
|
62
|
+
ログ
|
63
|
+
question GET /question(.:format) question#show
|
64
|
+
PATCH /question(.:format) question#update
|