こんにちは。
現在プログラミングの構築を行なっており、その中で現在showからshowに移動するプログラムを書いています。
<% if prev_customer = @customer.prev_customer %> <%= link_to "前へ", customer_path(prev_customer), class: "prev btn btn-danger" %> <% end %> <% if next_customer = @customer.next_customer %> <%= link_to "次へ", customer_path(next_customer), class: "next btn btn-danger" %> <% end %>
具体的なプログラムは上記となります。
modelでは
def next_customer Customer.where("id > ?", id).first end def prev_customer Customer.where("id < ?", id).last end
と記載しております。
これでも
show/1, show/2, show/3と移動するのですが、これから導入したいこととして、ransackで検索されたindexを元にしてshowを反映させたいと考えております。
例えばransackで検索して、
show/5, show/19, show/23
がindexに抽出された場合、showから直接移動しても上記のように移動するようにしたいのですが、現行のプログラムでは、
show/5, show/6, show/7
と次の数字に移動してしまいます。
要はindexで検索された内容がshowで解除されてしまうため、ransackの条件をshowでも残したいと考えております。
ここを解決する方法は何かないでしょうか?
def index @type = params[:type] @q = Customer.ransack(params[:q]) @customers = @q.result.page(params[:page]).per(100) end def show @q = Customer.ransack(params[:q]) @customer = @q.result.find(params[:id]) end
indexの検索条件は以下のようになります。
<div class="heading"><h2>リスト検索</h2></div> <!--検索機能実装--> <%= search_form_for @q do |f| %> <table width = "90%"> <col width="20%"> <col width="30%"> <col width="20%"> <col width="30%"> <tbody> <tr> <th colspan = "4">検索</th> </tr> <tr> <th>会社名</th> <td><%= f.search_field :company_cont, type: "text" %></td> <th>店舗名</th> <td><%= f.search_field :store_cont, type: "text" %> </td> </tr> <tr> <th>代表者</th> <td><%= f.search_field :first_name_cont, type: "text" %></td> <th>ダイヒョウ</th> <td><%= f.search_field :first_kana_cont, type: "text" %></td> </tr> <tr> <th>電話番号1</th> <td><%= f.search_field :tel_cont, type: "text" %></td> <th>電話番号2</th> <td><%= f.search_field :tel2_cont, type: "text" %></td> </tr> <tr> <th>FAX番号</th> <td><%= f.search_field :fax_cont, type: "text" %></td> <th>業種</th> <td><%= f.search_field :industry_cont, type: "text" %></td> </tr> <tr> <th>メール</th> <td><%= f.search_field :mail_cont, type: "text" %></td> <th>URL</th> <td><%= f.search_field :url_cont, type: "text" %></td> </tr> <tr> <th>人数</th> <td><%= f.search_field :people_cont, type: "text" %></td> <th>住所</th> <td><%= f.search_field :address_cont, type: "text" %></td> </tr> <!-- #TPD #2018.12.14 --> <tr> <th>最終コール状態</th> <td><%= select_tag "last_call[statu]", options_for_select( [""] + Call.StatuItems, @last_call_params[:statu] ) %></td> <th>再コール日時</th> <td></td> </th> <tr> <th>最終コール日時(最初)</th> <td> <div class='input-group date' id='created_at_from_datetimepicker'> <%= text_field_tag "last_call[created_at_from]", @last_call_params[:created_at_from], class: "form-control" %> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </td> <th>最終コール日時(最後)</th> <td> <div class='input-group date' id='created_at_to_datetimepicker'> <%= text_field_tag "last_call[created_at_to]", @last_call_params[:created_at_to], class: "form-control" %> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> <%# text_field_tag "last_detail[created_at_from]", @last_detail_params[:created_at_from], class: "datetimepicker" %> <%# text_field_tag "last_detail[created_at_to]", @last_detail_params[:created_at_to], class: "datetimepicker" %> </td> </tr> <tr> <th colspan = "4" ><%= f.submit '検索' %></th> </tr> <% end %> </tbody> </table>
追加修正
show
1<% if prev_customer = @customer.prev_customer %> 2 <%= link_to "前へ", customer_path(@prev_customer, q: params[:q]), class: "prev btn btn-danger" %> 3 <% end %> 4<% if next_customer = @customer.next_customer %> 5 <%= link_to "次へ", customer_path(@next_customer, q: params[:q]), class: "next btn btn-danger" %> 6 <% end %>
index
1<% @customers.each do |customer| %> 2 <tr> 3 <td><%= link_to customer.company, customer_path(customer, q: params[:q]) %></td> 4 <td><%= link_to customer.first_name, customer_path(customer, q: params[:q]) %></td> 5 <td><%= customer.tel %></td> 6 <td><%= customer.mail %></td> 7 <td><%= link_to '編集', edit_customer_path(customer), class: 'command'%> 8 <%= link_to '削除', 9 customer_path(customer), 10 method: :delete, 11 class: 'command', 12 data: { confirm: '本当に削除しますか?'} %></td> 13 <% end %>
controller
1 def before 2 @before_customer = Customer.where("id > ?", id).first 3 end 4 5 def next 6 @next_customer = Customer.where("id < ?", id).last 7 end 8 9 def show 10 @q = Customer.ransack(params[:q]) 11 @customer = @q.result.find(params[:id]) 12 end 13
回答2件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/08/21 16:21
2019/08/21 16:26