質問編集履歴

1

情報を追加いたしました。何卒宜しく御願い致します。

2020/02/29 01:45

投稿

raigakun
raigakun

スコア7

test CHANGED
File without changes
test CHANGED
@@ -207,3 +207,221 @@
207
207
 
208
208
 
209
209
  以上、お手数ですが、何卒ご教授の程宜しく御願い致します。
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+ #追記1
220
+
221
+ ###grop の場合と 個人の場合で createに渡ってくる情報が各々どうなのか、というデータ
222
+
223
+ 以下、task_controller内のcreateアクションにてbinding.pryを追記しました。
224
+
225
+ group_idの有無で条件分岐を試みてます。
226
+
227
+
228
+
229
+ ```task_controller.rb
230
+
231
+ class TaskController < ApplicationController
232
+
233
+
234
+
235
+ before_action :set_task, only: [:edit, :update, :destroy]
236
+
237
+
238
+
239
+ def index
240
+
241
+ @group = Group.find_by(id: params[:group_id])
242
+
243
+ end
244
+
245
+
246
+
247
+ def new
248
+
249
+ @group = Group.find_by(id: params[:group_id])
250
+
251
+ @task = Task.new
252
+
253
+ end
254
+
255
+
256
+
257
+ def create
258
+
259
+ binding.pry
260
+
261
+ if @group = Group.find_by(id: params[:group_id])
262
+
263
+ @task = @group.tasks.new(task_params)
264
+
265
+ else
266
+
267
+ @task = Task.new(task_params)
268
+
269
+ end
270
+
271
+ if @task.save
272
+
273
+ redirect_to group_task_index_path(@group)
274
+
275
+ else
276
+
277
+ render :new
278
+
279
+ end
280
+
281
+ end
282
+
283
+
284
+
285
+ def edit
286
+
287
+ @group = Group.find_by(id: params[:group_id])
288
+
289
+ end
290
+
291
+
292
+
293
+ def update
294
+
295
+ if @task.update_attributes(task_params)
296
+
297
+ redirect_to :root
298
+
299
+ else
300
+
301
+ render :edit
302
+
303
+ end
304
+
305
+ end
306
+
307
+
308
+
309
+ def destroy
310
+
311
+ @task.destroy
312
+
313
+ redirect_to :root
314
+
315
+ end
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+ private
324
+
325
+ def task_params
326
+
327
+ params.require(:task).permit(:title).merge(user_id: current_user.id)
328
+
329
+ end
330
+
331
+
332
+
333
+ def set_task
334
+
335
+ @task = Task.find_by(id: params[:id])
336
+
337
+ end
338
+
339
+ end
340
+
341
+
342
+
343
+ ```
344
+
345
+
346
+
347
+ ###個人でタスクを新規作成
348
+
349
+ [実際のビュー](https://gyazo.com/90561c2dbb2bf0bb0b342be36395313b)
350
+
351
+ [binding.pry後、ターミナルの画面](https://gyazo.com/906c3a5db7fef992b1b3a936ec24965f)
352
+
353
+
354
+
355
+ ###グループでタスクを新規作成
356
+
357
+ [実際のビュー](https://gyazo.com/795b0c4eaf0f5ed6fdf58c6b0e18543f)
358
+
359
+ [binding.pry後、ターミナルの画面](https://gyazo.com/e04a20cd3e1bcf17656bcf94a9c15746)
360
+
361
+
362
+
363
+ ###タスクの新規作成のコード(個人とグループでviewが異なる様に、条件分岐を用いております。)
364
+
365
+ ```app/views/task/new.html.erb
366
+
367
+ <% - if @group.present?%>
368
+
369
+ <header class="header2">
370
+
371
+ <nav class="nav2">
372
+
373
+ <ul class="header_menu2">
374
+
375
+ <li class="nav-link">
376
+
377
+ <%= @group.name %>
378
+
379
+ </li>
380
+
381
+ <ul class="header_menu2_inner2">
382
+
383
+ <li><%= link_to "Create Group Tasks", new_group_task_path(@group), class: "nav-link listNew2", method: :get %></li>
384
+
385
+ <li><%= link_to "Edit Group",edit_group_path(@group), class: "nav-link listNew3", method: :get %></li>
386
+
387
+ </ul>
388
+
389
+ </ul>
390
+
391
+ </nav>
392
+
393
+ </header>
394
+
395
+ <div class="tasknewPage">
396
+
397
+ <div class="container">
398
+
399
+ <%= form_with model: @task, url: { action: :create }, class: "new_task", local: true do |f| %>
400
+
401
+ <%= f.label :title %>
402
+
403
+ <%= f.text_field :title, autofocus: true, class: "form-control taskName", placeholder: "タスク名" %>
404
+
405
+ <div class="text-center"><%= f.submit "作成", class: "submitBtn" %></div>
406
+
407
+ <% end %>
408
+
409
+
410
+
411
+ <% else %>
412
+
413
+ aaa
414
+
415
+ <%= form_with model: @tasks, url: { action: :create }, class: "new_task", local: true do |f| %>
416
+
417
+ <%= f.label :title %>
418
+
419
+ <%= f.text_field :title, autofocus: true, class: "form-control taskName", placeholder: "タスク名" %>
420
+
421
+ <div class="text-center"><%= f.submit "作成", class: "submitBtn" %></div>
422
+
423
+ <% end %>
424
+
425
+ <% end %>
426
+
427
+ ```