複数条件の検索機能
indexで表示されているデータに、複数の検索をかけて絞り込みたいです。現在は、ひとつの条件だけ検索できる状態です。
Active_Hashを利用しているデータもあり、それらはプルダウンでの検索を望んでいます。
以下は、現在の状態です。
DB設計
棋譜テーブル
・日付(data)
・相手(string)
・結果(integer)・・・Active_Hash
・戦型(integer)・・・Active_Hash
・棋譜(text)
コード
app/models/kifu.rb
ruby
1class Kifu < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to_active_hash :result 4 belongs_to_active_hash :type 5 6 with_options presence: true do 7 validates :date 8 validates :opponent 9 validates :result_id, numericality: { other_than: 1 } 10 validates :type_id, numericality: { other_than: 1 } 11 validates :kifu 12 end 13 14 def self.search(search) 15 if search != "" 16 Kifu.where('opponent LIKE(?)', "%#{search[:keyword]}%").where(result_id: search[:result]).where(type_id: search[:type]) 17 else 18 Kifu.all 19 end 20 end 21 22end
db/migrate/20201020172101_create_kifus.rb
ruby
1class CreateKifus < ActiveRecord::Migration[6.0] 2 def change 3 create_table :kifus do |t| 4 t.date :date, null: false 5 t.string :opponent, null: false 6 t.integer :result_id, null: false 7 t.integer :type_id, null: false 8 t.text :kifu, null: false 9 10 t.timestamps 11 end 12 end 13end
app/models/result.rb
ruby
1class Result < ActiveHash::Base 2 self.data = [ 3 { id: 1, name: '--' }, 4 { id: 2, name: '○' }, 5 { id: 3, name: '●' }, 6 { id: 4, name: '千' }, 7 { id: 5, name: '持' } 8 ] 9end
app/models/type.rb
ruby
1class Type < ActiveHash::Base 2 self.data = [ 3 { id: 1, name: '--' }, 4 { id: 2, name: '矢倉' }, 5 { id: 3, name: '横歩取り' }, 6 { id: 4, name: '角換わり腰掛け銀' }, 7 { id: 5, name: '角換わり早繰り銀' }, 8 { id: 6, name: '角換わり棒銀' }, 9 { id: 7, name: '角換わり右玉' }, 10 { id: 8, name: '一手損角換わり' }, 11 { id: 9, name: '相掛かり' }, 12 { id: 10, name: '雁木' }, 13 { id: 11, name: '嬉野流' }, 14 { id: 12, name: '中飛車' }, 15 { id: 13, name: '四間飛車' }, 16 { id: 14, name: '三間飛車' }, 17 { id: 15, name: '向かい飛車' }, 18 { id: 16, name: '角交換振り飛車' }, 19 { id: 17, name: 'その他' } 20 ] 21end
config/routes.rb
ruby
1Rails.application.routes.draw do 2 root to: 'kifus#index' 3 resources :kifus do 4 collection do 5 get 'search' 6 end 7 end 8end
app/controllers/kifus_controller.rb
ruby
1class KifusController < ApplicationController 2 before_action :set_kifu, only: [:edit, :show, :destroy, :update] 3 4 def index 5 @kifus = Kifu.all.order('date DESC') 6 end 7 8 def new 9 @kifu = Kifu.new 10 end 11 12 def create 13 @kifu = Kifu.create(kifu_params) 14 if @kifu.valid? 15 @kifu.save 16 redirect_to root_path 17 else 18 render 'new' 19 end 20 end 21 22 def edit 23 end 24 25 def update 26 if @kifu.update(kifu_params) 27 redirect_to kifu_path(@kifu.id) 28 else 29 render 'edit' 30 end 31 end 32 33 def show 34 end 35 36 def destroy 37 if @kifu.destroy 38 redirect_to root_path 39 else 40 redirect_to kifu_path(@kifu.id) 41 end 42 end 43 44 def search 45 @kifus = Kifu.search(params) 46 end 47 48 private 49 50 def kifu_params 51 params.require(:kifu).permit(:date, :opponent, :result_id, :type_id, :kifu) 52 end 53 54 def set_kifu 55 @kifu = Kifu.find(params[:id]) 56 end 57 58end
app/views/kifus/index.html.erb
html
1<div class="index-wrapper"> 2 3 <div class="new-link"> 4 <%= link_to '棋譜投稿ページへ', new_kifu_path %> 5 </div> 6 7 <h2><棋譜検索></h2> 8 <%= form_with(url: search_kifus_path, local: true, method: :get, class: "search-form") do |f| %> 9 10 <div class="selection-field"> 11 <%= f.label :opponent, '相手:' %> 12 <%= f.text_field :keyword, placeholder: "名前の一部でも", class: "search-input" %> 13 </div> 14 15 <div class="selection-field"> 16 <%= f.label :result_id, '結果:' %> 17 <%= f.collection_select :result_id, Result.all, :id, :name, {} %> 18 </div> 19 20 <div class="selection-field"> 21 <%= f.label :type_id, '戦型:' %> 22 <%= f.collection_select :type_id, Type.all, :id, :name, {} %> 23 </div> 24 25 <%= f.submit "????", class: "search-btn" %> 26 27 <h2><棋譜一覧></h2> 28 29 <div class="kifu-lists"> 30 <% @kifus.each do |kifu| %> 31 <div class="one-kifu"> 32 33 <%# 日付 %> 34 <span class="date"> 35 <%= kifu.date %> 36 </span> 37 38 <%# 相手 %> 39 <span class="opponent"> 40 <%= kifu.opponent %> 41 </span> 42 43 <%# 結果 %> 44 <span class="result"> 45 <%= kifu.result.name %> 46 </span> 47 48 <%# 戦型 %> 49 <span class="type"> 50 <%= kifu.type.name %> 51 </span> 52 53 <%# 棋譜リンク %> 54 <span class="kifu-page"> 55 <%= link_to '棋譜', kifu_path(kifu.id) %> 56 </span> 57 58 </div> 59 <% end %> 60 </div> 61</div>
app/assets/stylesheets/index.scss
scss
1.index-wrapper { 2 margin: 20px; 3} 4 5.new-link { 6 font-size: 20px; 7} 8 9h2 { 10 margin-top: 20px; 11} 12 13.search-form { 14 margin: 5px 0px; 15} 16 17.selection-field { 18 margin-bottom: 5px; 19} 20 21.kifu-lists { 22 width: 550px; 23 margin-top: 5px; 24} 25 26.one-kifu { 27 padding: 5px; 28 margin-bottom: 5px; 29 border: 1px solid black; 30 font-size: 20px; 31 position: relative; 32} 33 34.opponent { 35 left: 150px; 36 position: absolute; 37} 38 39.result { 40 left: 250px; 41 position: absolute; 42} 43 44.type { 45 left: 300px; 46 position: absolute; 47} 48 49.kifu-page { 50 right: 10px; 51 position: absolute; 52}
app/views/kifus/search.html.erb
html
1// indexと同じコード
調べた内容
・【Rails】複数の条件の検索機能を作る。日付範囲検索を行う
・【Rails】キーワード追加検索、絞り込み検索
検索をかけても、search.html.erbに飛んで棋譜がひとつも出てきません。原因をご教授いただきたいです。
バージョン情報
ruby 2.6.5p114
Rails 6.0.3.4