現在Rubyで検索条件の設定を行っております。
検索条件には、
・一定期間を検索
(ex.2016/11/01~2017/09/01を検索)
・更新回数を検索
(1:nの関係において、nが登録された回数を指定した検索)
を搭載したく考えておりますが、まだrubyが未熟な部分もあり、どのようにmodel、viewの設定をすれば良いかわかりません。
どなたか教えて頂けると幸いです。
よろしくお願い致します。
index.html.erb
1 2<!--検索機能実装--> 3<%= search_form_for @q do |f| %> 4 <table width = "90%"> 5 <col width="20%"> 6 <col width="30%"> 7 <col width="20%"> 8 <col width="30%"> 9 <tbody> 10 <tr> 11 <th colspan = "4">検索</th> 12 </tr> 13 <tr> 14 <th>会社名</th> 15 <td><%= f.search_field :company_cont, type: "text" %></td> 16 <th>店舗名</th> 17 <td><%= f.search_field :store_cont, type: "text" %> </td> 18 </tr> 19 <tr> 20 <th>代表者</th> 21 <td><%= f.search_field :owner_cont, type: "text" %></td> 22 <th>ダイヒョウ</th> 23 <td><%= f.search_field :kana_cont, type: "text" %></td> 24 </tr> 25 <tr> 26 <th>電話番号1</th> 27 <td><%= f.search_field :tel_cont, type: "text" %></td> 28 <th>電話番号2</th> 29 <td><%= f.search_field :tel2_cont, type: "text" %></td> 30 </tr> 31 <tr> 32 <th>FAX番号</th> 33 <td><%= f.search_field :fax_cont, type: "text" %></td> 34 <th>業種</th> 35 <td><%= f.search_field :industry_cont, type: "text" %></td> 36 </tr> 37 <tr> 38 <th>メール</th> 39 <td><%= f.search_field :mail_cont, type: "text" %></td> 40 <th>URL</th> 41 <td><%= f.search_field :url_cont, type: "text" %></td> 42 </tr> 43 <tr> 44 <th>人数</th> 45 <td><%= f.search_field :people_cont, type: "text" %></td> 46 <th>住所</th> 47 <td><%= f.search_field :address_cont, type: "text" %></td> 48 </tr> 49 <tr> 50 <th>期間</th> 51 <td><%= f.search_field :date_first, type: "date" %></td> 52 <th>期間</th> 53 <td><%= f.search_field :date_last, type: "date" %></td> 54 </tr> 55 <tr> 56 <th colspan = "4" ><%= f.submit '検索' %></th> 57 </tr> 58 <% end %> 59 </tbody> 60 </table>
model
1 def self.search(search) #self.でクラスメソッドとしている 2 if search # Controllerから渡されたパラメータが!= nilの場合は、titleカラムを部分一致検索 3 Post.where( 4 ['company LIKE ?', "%#{company}%"], 5 ['store LIKE ?', "%#{store}%"], 6 ['owner LIKE ?', "%#{owner}%"], 7 ['kana LIKE ?', "%#{kana}%"], 8 ['tel LIKE ?', "%#{tel}%"], 9 ['tel2 LIKE ?', "%#{tel2}%"], 10 ['fax LIKE ?', "%#{fax}%"], 11 ['industry LIKE ?', "%#{industry}%"], 12 ['mail LIKE ?', "%#{mail}%"], 13 ['url LIKE ?', "%#{url}%"], 14 ['people LIKE ?', "%#{people}%"], 15 ['address LIKE ?', "%#{address}%"], 16 ['statu LIKE ?', "%#{statu}%"], 17 ['time LIKE ?', "%#{time}%"], 18 ['comment LIKE ?', "%#{comment}%"], 19 ['created_at BETWEEN ? AND ?', from, to, "%#{created_at}%"], 20 ) 21 22 else 23 Post.all #全て表示。 24 end 25 end 26
あなたの回答
tips
プレビュー