前提・実現したいこと
データ一覧画面に検索機能を実装後、kaminariを利用してページネーションを実装しました。
その後、検索機能で検索ワードを入力し、検索しても、表示はすべてのデータが表示され、検索結果でソートされたデータのみの表示がされなくなりました。
ページネーションの実装の記載をコメントアウトすると正常動作します。
原因としてはページネーションの機能がDBのすべてのデータを対象としていることが原因で、正しくは検索条件にマッチするデータのみをページネーションの対象とする必要があると思うのですが、実装方法のサンプルが見つからずに困っています。
そのため、実装例をご教示頂けると大変助かります。
該当のソースコード
index.html.erb
Ruby
1<main> 2<!-- ↓↓ログインメッセージ、データ登録、編集成功表示↓↓ --> 3 4 <% if flash[:notice] %> 5 <div class="alert alert-success"> 6 <%= flash[:notice] %> 7 </div> 8 <% end %> 9 10<!-- ↑↑ログインメッセージ、データ登録、編集成功表示↑↑ --> 11 12 13<br> 14<br> 15<br> 16<p class="rei1">通知</p> 17<br> 18<br> 19 20<% @items_1month.each do |item| %> 21 <ul> 22 <li> 23 管理ID <%= item.manage_id %> は期限切れまであとXX日です。有効期限の終了日は <%= item.end_date %> です。 24 </li> 25 </ul> 26<% end %> 27 28 29<br> 30<p class="rei1">データ検索</p> 31 32<div> 33<br> 34<%= form_tag('/items', method: 'get') do %> 35 <%= label_tag(:manage_id, '管理ID') %> 36 <%= text_field_tag(:manage_id) %> 37 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 38<% end %> 39 40<br> 41<%= form_tag('/items', method: 'get') do %> 42 <%= label_tag(:category_id, 'カテゴリ') %> 43 <%= text_field_tag(:category_id) %> 44 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 45<% end %> 46 47<br> 48<%= form_tag('/items', method: 'get') do %> 49 <%= label_tag(:publish_corporation, '発行会社') %> 50 <%= text_field_tag(:publish_corporation) %> 51 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 52<% end %> 53 54<br> 55<%= form_tag('/items', method: 'get') do %> 56 <%= label_tag(:ip_address, 'IPアドレス') %> 57 <%= text_field_tag(:ip_address) %> 58 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 59<% end %> 60 61<br> 62<%= form_tag('/items', method: 'get') do %> 63 <%= label_tag(:target_server, '対象サーバ') %> 64 <%= text_field_tag(:target_server) %> 65 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 66<% end %> 67 68<br> 69<%= form_tag('/items', method: 'get') do %> 70 <%= label_tag(:start_date, '有効期限の開始日') %> 71 <%= text_field_tag(:start_date) %> 72 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 73<% end %> 74 75<br> 76<%= form_tag('/items', method: 'get') do %> 77 <%= label_tag(:end_date, '有効期限の終了日') %> 78 <%= text_field_tag(:end_date) %> 79 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 80<% end %> 81 82<br> 83<%= form_tag('/items', method: 'get') do %> 84 <%= label_tag(:comment, 'コメント') %> 85 <%= text_field_tag(:comment) %> 86 <%= submit_tag('検索') %> <%= link_to 'クリア', items_path %> 87<% end %> 88</div> 89 90<br> 91<p class="rei1">データ一覧</p> 92<br> 93 94<!-- 権限によってデータ一覧の項目値を変更 --> 95 96<section> 97 98<!-- 追加箇所開始 --> 99<% if false %> 100<%= page_entries_info @items %> 101<% end %> 102<!-- 追加箇所終了 --> 103 104 105 <table class="type06"> 106 <thead> 107 108 <% if session[:authority] == "1" %> 109 110 <tr> 111 <th>管理ID</th> 112 <th>カテゴリ</th> 113 <th>発行会社</th> 114 <th>IPアドレス</th> 115 <th>対象サーバ</th> 116 <th>有効期限の開始日</th> 117 <th>有効期限の終了日</th> 118 <th>コメント</th> 119 <th>操作</th> 120 </tr> 121 122 </thead> 123 <tbody> 124 125 <% @items.each do |item| %> 126 <tr> 127 <td><%= item.manage_id %></td> 128 <td><%= item.category_id %></td> 129 <td><%= item.publish_corporation %></td> 130 <td><%= item.ip_address %></td> 131 <td><%= item.target_server %></td> 132 <td><%= item.start_date %></td> 133 <td><%= item.end_date %></td> 134 <td><%= item.comment %></td> 135 <td> 136 <%= link_to '[Edit]', edit_item_path(item), class: 'command' %> 137 <%= link_to '[Delete]', 138 item_path(item), 139 method: :delete, 140 class: 'command', 141 data: { confirm: 'データ削除しますか?' } %> 142 </td> 143 </tr> 144 145 <% end %> 146 147 148 <% elsif session[:authority] == "2" %> 149 150 <tr> 151 <th>管理ID</th> 152 <th>カテゴリ</th> 153 <th>発行会社</th> 154 <th>IPアドレス</th> 155 <th>対象サーバ</th> 156 <th>有効期限の開始日</th> 157 <th>有効期限の終了日</th> 158 <th>コメント</th> 159 </tr> 160 161 <% @items.each do |item| %> 162 <tr> 163 <td><%= item.manage_id %></td> 164 <td><%= item.category_id %></td> 165 <td><%= item.publish_corporation %></td> 166 <td><%= item.ip_address %></td> 167 <td><%= item.target_server %></td> 168 <td><%= item.start_date %></td> 169 <td><%= item.end_date %></td> 170 <td><%= item.comment %></td> 171 </tr> 172 173 <% end %> 174 175 <% else %> 176 177 <!-- データ一覧を表示しない --> 178 179 <% end %> 180 181 </tbody> 182 </table> 183 184 185<!-- 追加箇所開始 --> 186 187<%= paginate @items %> 188 189<!-- 追加箇所終了 --> 190 191</section> 192 193<br> 194<%= link_to "CSV出力", items_path(format: :csv), class: "btn btn-primary" %> 195 196</main> 197
items_controller.rb
Ruby
1class ItemsController < ApplicationController 2 3 before_action :authenticate_user 4 5 6 def index 7 8 # データを降順で表示する 9 # @items = GyomuDatum.all.order(created_at: 'desc') 10 11 # データ検索 12 13 if params[:manage_id] 14 @items = GyomuDatum.where('manage_id LIKE ?', "%#{params[:manage_id]}%") 15 elsif params[:category_id] 16 @items = GyomuDatum.where('category_id LIKE ?', "%#{params[:category_id]}%") 17 elsif params[:publish_corporation] 18 @items = GyomuDatum.where('publish_corporation LIKE ?', "%#{params[:publish_corporation]}%") 19 elsif params[:ip_address] 20 @items = GyomuDatum.where('ip_address LIKE ?', "%#{params[:ip_address]}%") 21 elsif params[:target_server] 22 @items = GyomuDatum.where('target_server LIKE ?', "%#{params[:target_server]}%") 23 elsif params[:start_date] 24 @items = GyomuDatum.where('start_date LIKE ?', "%#{params[:start_date]}%") 25 elsif params[:end_date] 26 @items = GyomuDatum.where('end_date LIKE ?', "%#{params[:end_date]}%") 27 elsif params[:comment] 28 @items = GyomuDatum.where('comment LIKE ?', "%#{params[:comment]}%") 29 else 30 @items = GyomuDatum.all.order(created_at: 'desc') 31 end 32 33 34 35 # ページネーション 36 @items = GyomuDatum.page(params[:page]).per(20) 37 38 # 期限が迫っているデータを検索する 39 40 # 1ヶ月前 41 from = Time.now.at_beginning_of_day 42 to = (from + 1.month) 43 @items_1month = GyomuDatum.where(end_date: from...to) 44 45 end 46end
補足情報(FW/ツールのバージョンなど)
・Phusion Passenger
Phusion Passenger 5.2.3
・Apache
Server version: Apache/2.4.6 (CentOS)
・Rails
Rails 4.2.10
・OSバージョン
CentOS Linux release 7.4.1708 (Core)
・Mysql
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
・Git
git version 1.8.3.1
・Ruby
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-linux]
回答1件
あなたの回答
tips
プレビュー