質問編集履歴

1

コントローラーのコードを追加

2023/03/06 15:27

投稿

ruler
ruler

スコア2

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,49 @@
9
9
 
10
10
  ### 該当のソースコード
11
11
 
12
+ ```controller.rb
13
+ class HealthsController < ApplicationController
14
+ before_action :set_q
15
+ def top
16
+ end
17
+
18
+ def index
19
+ @health=Health.all
20
+ @results=@q.result
21
+ end
22
+
23
+ def show
24
+ @health=Health.all
25
+ @results=@q.result
26
+ end
27
+
28
+ def new
29
+ @health=Health.new
30
+ @date=Date.today
31
+ end
32
+
33
+ def create
34
+ health=Health.new(health_params)
35
+ health.save!
36
+ redirect_to(healths_url, notice: "送信を完了しました。")
37
+ end
38
+
39
+ def search
40
+ @results=@q.result
41
+ end
42
+
43
+ private
44
+
45
+ def health_params
46
+ params.require(:health).permit(:year, :school_class, :class_number, :name, :commute, :temperature, :etc)
47
+ end
48
+
49
+ def set_q
50
+ @q=Health.ransack(params[:q])
51
+ end
52
+ end
53
+
54
+ ```
12
55
  ```Ruby on rails7.0.4.2(new.html.erbファイルの中身)
13
56
  <h1>健康観察アプリ</h1>
14
57
  <% require "date" %>
@@ -66,3 +109,7 @@
66
109
  Time::DATE_FORMATS[:default]="%Y-%m-%d"
67
110
  と書いたファイルを作成しています。
68
111
 
112
+ <追記>
113
+ コントローラーのコードを追加しました。
114
+ 今回の質問に該当するアクションはnewアクションとcreateアクションで
115
+ newアクションはget, createアクションはpostをルーティングで指定しています。