teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問追加(ransackでのメソッドを定義する方法)

2020/12/24 15:24

投稿

shogohori1988
shogohori1988

スコア0

title CHANGED
File without changes
body CHANGED
@@ -75,4 +75,55 @@
75
75
  .
76
76
  = f.submit '検索する', class: 'btn btn-primary'
77
77
 
78
+ ```
79
+
80
+ ## 追記(2020/12/24)
81
+ DBの相談テーブルのchild_age(年齢)、child_moon_age(月齢)をカラムを削除し、child_age_moon_age(月齢の累積)として管理することとしました。出入力に関しては年齢、月齢を分けるために相談モデル(app/models/consultation.rb)に``attr_accessor :age, :moon_age``追加しました。
82
+ しかし、ransackで``:age, :moon_age``メソッドを定義する方法がわかりません。
83
+ どのようにすれば定義できるのか教えていただきたく、何卒お願いします。
84
+
85
+ 【ビュー】
86
+ ```slim
87
+ = search_form_for @q do |f|
88
+ .
89
+ .
90
+ .form-inline
91
+ .col
92
+ = f.label :age, '年齢:'
93
+ .col
94
+ = f.select :age_gteq , options_for_select((0..3).to_a), { include_blank: '選択'}, class: 'selectpicker form-control'
95
+ | 歳
96
+ .form-inline
97
+ .col
98
+ = f.label :moon_age, '月齢:'
99
+ .col
100
+ = f.select :moon_age_gteq, options_for_select((0..11).to_a), { include_blank: '選択'}, class: 'selectpicker form-control'
101
+ | ヶ月
102
+ | 〜
103
+ .form-inline
104
+ .col
105
+ = f.label :age, '年齢:'
106
+ .col
107
+ = f.select :age_lteq, options_for_select((0..3).to_a), { include_blank: '選択'}, class: 'selectpicker form-control'
108
+ | 歳
109
+ .form-inline
110
+ .col
111
+ = f.label :moon_age, '月齢:'
112
+ .col
113
+ = f.select :moon_age_lteq, options_for_select((0..11).to_a), { include_blank: '選択'}, class: 'selectpicker form-control'
114
+ .
115
+ .
116
+ = f.submit '検索する', class: 'btn btn-primary'
117
+ ```
118
+
119
+ 【エラー】
120
+ ```
121
+ ActionView::Template::Error (undefined method `age_gteq' for #<Ransack::Search:0x00007fbf79f8ff80>):
122
+ 24: .col
123
+ 25: = f.label :age, '年齢:'
124
+ 26: .col
125
+ 27: = f.select :age_gteq , options_for_select((0..3).to_a), { include_blank: '選択'}, class: 'selectpicker form-control'
126
+ 28: | 歳
127
+ 29: .form-inline
128
+ 30: .col
78
129
  ```