質問編集履歴

4

controllerのsearch_params, User::HobbySearchFormのinitializeを変更しました

2021/10/11 10:05

投稿

annasui
annasui

スコア1

test CHANGED
File without changes
test CHANGED
@@ -460,7 +460,7 @@
460
460
 
461
461
  ```
462
462
 
463
- ### controllers/user/hobbies_controller.rb
463
+ ### controllers/user/hobbies_controller.rb(修正後)
464
464
 
465
465
  ```
466
466
 

3

controllerのsearch_params, User::HobbySearchFormのinitializeを変更しました

2021/10/11 10:05

投稿

annasui
annasui

スコア1

test CHANGED
File without changes
test CHANGED
@@ -104,7 +104,7 @@
104
104
 
105
105
  ```
106
106
 
107
- ### form/user/hobby_search_form.rb
107
+ ### form/user/hobby_search_form.rb(修正後)
108
108
 
109
109
  ```
110
110
 
@@ -118,6 +118,24 @@
118
118
 
119
119
 
120
120
 
121
+ def initialize(search_params = {})
122
+
123
+ @name = search_params[:name]
124
+
125
+ @initial_investment = search_params[:initial_investment]
126
+
127
+ @running_cost = search_params[:running_cost]
128
+
129
+ @frequency = search_params[:frequency]
130
+
131
+ @place = search_params[:place]
132
+
133
+ @number = search_params[:number]
134
+
135
+ end
136
+
137
+
138
+
121
139
  def search
122
140
 
123
141
  rel = Hobby
@@ -510,11 +528,7 @@
510
528
 
511
529
  def search_params
512
530
 
513
- params[:search]&.permit([
514
-
515
- :name, :initial_investment, :running_cost, :frequency, :place, :number
531
+ params.permit(:search).permit(:name, :initial_investment, :running_cost, :frequency, :place, :number)
516
-
517
- ])
518
532
 
519
533
  end
520
534
 

2

検索した時にコマンドに表示されるログを追記しました

2021/10/11 10:04

投稿

annasui
annasui

スコア1

test CHANGED
File without changes
test CHANGED
@@ -521,3 +521,7 @@
521
521
  end
522
522
 
523
523
  ```
524
+
525
+ ### 検索時のログ
526
+
527
+ Hobby Load (0.6ms) SELECT "hobbies".* FROM "hobbies" WHERE "hobbies"."name" = $1 ORDER BY "hobbies"."id" ASC [["name", "ランニング"]]

1

すみません、controllerを忘れていました

2021/10/11 01:15

投稿

annasui
annasui

スコア1

test CHANGED
File without changes
test CHANGED
@@ -426,6 +426,8 @@
426
426
 
427
427
  end
428
428
 
429
+
430
+
429
431
  end
430
432
 
431
433
 
@@ -439,3 +441,83 @@
439
441
  end
440
442
 
441
443
  ```
444
+
445
+ ### controllers/user/hobbies_controller.rb
446
+
447
+ ```
448
+
449
+ class User::HobbiesController < User::Base
450
+
451
+
452
+
453
+
454
+
455
+ def index
456
+
457
+ @search_form = User::HobbySearchForm.new(search_params)
458
+
459
+ @hobbies = @search_form.search
460
+
461
+ end
462
+
463
+
464
+
465
+ def new
466
+
467
+ @hobby = Hobby.new
468
+
469
+ end
470
+
471
+
472
+
473
+ def create
474
+
475
+ @hobby = Hobby.new(hobby_params)
476
+
477
+ if @hobby.save
478
+
479
+ redirect_to user_hobby_path(@hobby.id), notice: "Hobby #{@hobby.name}を登録しました"
480
+
481
+ else
482
+
483
+ render "new"
484
+
485
+ end
486
+
487
+ end
488
+
489
+
490
+
491
+ def show
492
+
493
+ @hobby = Hobby.find_by(params[:hobby_id])
494
+
495
+ end
496
+
497
+
498
+
499
+ private
500
+
501
+
502
+
503
+ def hobby_params
504
+
505
+ params.permit(:name, :initial_investment, :running_cost, :frequency, :place, :number)
506
+
507
+ end
508
+
509
+
510
+
511
+ def search_params
512
+
513
+ params[:search]&.permit([
514
+
515
+ :name, :initial_investment, :running_cost, :frequency, :place, :number
516
+
517
+ ])
518
+
519
+ end
520
+
521
+ end
522
+
523
+ ```