質問編集履歴

1

view,controllerのソースコード追記

2019/12/13 01:21

投稿

ktanizaw
ktanizaw

スコア5

test CHANGED
File without changes
test CHANGED
@@ -102,6 +102,252 @@
102
102
 
103
103
 
104
104
 
105
+ ```ruby
106
+
107
+ class TasksController < ApplicationController
108
+
109
+ before_action :set_task, only: [:show, :edit, :update, :destroy]
110
+
111
+
112
+
113
+ # GET /tasks
114
+
115
+ def index
116
+
117
+ @tasks = Task.all
118
+
119
+ @title = Task.group(:title).pluck(:title).sort
120
+
121
+ @tasks = Task.order(created_at: :desc)
122
+
123
+ if params[:sort_expired]
124
+
125
+ @tasks = Task.all.order(deadline: :desc)
126
+
127
+ end
128
+
129
+ end
130
+
131
+
132
+
133
+ def search
134
+
135
+ @tasks = Task.where('title LIKE ?', "%#{params[:title]}%")
136
+
137
+ @title = Task.group(:title).pluck(:title).sort
138
+
139
+ render :index
140
+
141
+ end
142
+
143
+
144
+
145
+ # GET /tasks/1
146
+
147
+ def show
148
+
149
+ @task = Task.find(params[:id])
150
+
151
+ end
152
+
153
+
154
+
155
+ # GET /tasks/new
156
+
157
+ def new
158
+
159
+ @task = Task.new
160
+
161
+ end
162
+
163
+
164
+
165
+ # GET /tasks/1/edit
166
+
167
+ def edit
168
+
169
+ end
170
+
171
+
172
+
173
+ # POST /tasks
174
+
175
+ def create
176
+
177
+ @task = Task.new(task_params)
178
+
179
+
180
+
181
+ if @task.save
182
+
183
+ redirect_to @task, notice: 'Task was successfully created.'
184
+
185
+ else
186
+
187
+ render :new
188
+
189
+ end
190
+
191
+ end
192
+
193
+
194
+
195
+ # PATCH/PUT /tasks/1
196
+
197
+ def update
198
+
199
+ if @task.update(task_params)
200
+
201
+ redirect_to @task, notice: 'Task was successfully updated.'
202
+
203
+ else
204
+
205
+ render :edit
206
+
207
+ end
208
+
209
+ end
210
+
211
+
212
+
213
+ # DELETE /tasks/1
214
+
215
+ def destroy
216
+
217
+ @task.destroy
218
+
219
+ redirect_to tasks_url, notice: 'Task was successfully destroyed.'
220
+
221
+ end
222
+
223
+
224
+
225
+ private
226
+
227
+ # Use callbacks to share common setup or constraints between actions.
228
+
229
+ def set_task
230
+
231
+ @task = Task.find(params[:id])
232
+
233
+ end
234
+
235
+
236
+
237
+ # Only allow a trusted parameter "white list" through.
238
+
239
+ def task_params
240
+
241
+ params.require(:task).permit(:title, :content, :rank, :deadline, :status)
242
+
243
+ end
244
+
245
+ end
246
+
247
+ ```
248
+
249
+
250
+
251
+ ```ruby
252
+
253
+
254
+
255
+ <h1>Tasks</h1>
256
+
257
+
258
+
259
+ <div class="">
260
+
261
+ <p>タイトル</p>
262
+
263
+ <%= form_tag(tasks_path, :method => 'get' ) do %>
264
+
265
+ <%= text_field_tag(:title) %>
266
+
267
+ <p>ステータス</p>
268
+
269
+ <%= select_tag(:status) %>
270
+
271
+ <%= submit_tag '検索'%>
272
+
273
+ <% end %>
274
+
275
+ </div>
276
+
277
+
278
+
279
+ <table>
280
+
281
+ <thead>
282
+
283
+ <tr>
284
+
285
+ <th>Title
286
+
287
+ </th>
288
+
289
+ <th>Content</th>
290
+
291
+ <th>Rank</th>
292
+
293
+ <th>Deadline</th>
294
+
295
+ <th>状態</th>
296
+
297
+ <th colspan="3"></th>
298
+
299
+ </tr>
300
+
301
+ </thead>
302
+
303
+
304
+
305
+ <tbody>
306
+
307
+ <% @tasks.each do |task| %>
308
+
309
+ <tr>
310
+
311
+ <td><%= task.title %></td>
312
+
313
+ <td><%= task.content %></td>
314
+
315
+ <td><%= task.rank %></td>
316
+
317
+ <td><%= task.deadline %></td>
318
+
319
+ <td><%= task.status %></td>
320
+
321
+ <td><%= link_to 'Show', task %></td>
322
+
323
+ <td><%= link_to 'Edit', edit_task_path(task) %></td>
324
+
325
+ <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
326
+
327
+ </tr>
328
+
329
+ <% end %>
330
+
331
+ </tbody>
332
+
333
+ </table>
334
+
335
+
336
+
337
+ <%= link_to "終了期限でソートする", tasks_path(sort_expired: "true") %>
338
+
339
+ <br>
340
+
341
+
342
+
343
+ <%= link_to 'New Task', new_task_path %>
344
+
345
+ ```
346
+
347
+
348
+
349
+
350
+
105
351
  ### 試したこと
106
352
 
107
353
 
@@ -164,4 +410,6 @@
164
410
 
165
411
 
166
412
 
413
+
414
+
167
415
  よろしくおねがいいたします。