質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

2回答

1034閲覧

show間で移動する際に、ransackが解除される問題の解決

KOO_

総合スコア58

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/08/20 02:52

編集2019/08/21 16:24

こんにちは。

現在プログラミングの構築を行なっており、その中で現在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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

prev_customer 達は modelのmethodになっていて、ransacを参照するようになっていないためかと思います。

似たようなことをやっています。

link_to custer_next_customer(@customer, q: params[:q])
とでもし、
method next_customer をコントローラに追加するという方法でどうでしょうか

#####
他にもいくつか問題が有ることにきがつきました。あとで時間ができたら追記し

### 追記
1) index.html に <%= link_to "詳細", customer_path(customer), というような記述があると思います。そこを customer_path(customer, q: params[:q]) にしてください。多分今はやっていないのでは?
showのviewとctrlでやり取りが必要ですが、それをindexから渡す必要があります

  1. 上にも書きましたが ransackを使うために、next,prevはmodelではなくctrlで行うほうがよいです。

ctrl show で @next_customer、@prev_customer を用意して渡す。
(これですと customer_path(@prev_customer, q: params[:q])でできるので新しいpath作らないで済みます)

以下おせっかい
次前の検索を id: で行ってますがこれ offset で行うと楽かな?と思います。将来 名前順にしたいとかにも対応しやすい。

@offset = params[:id] @customer = @q.result.offset(@offset).first

viewには@prev_customerでなく、@offsetをわたし、customer_path(@offset-1, q: params[:q])とかする。 id でなく offset を騙して渡すわけです。
ですから、index.htmlでも

@customers.each.with_index( (page - 1) * par_page ){|customer,offset| link_to 表示, customer_path(offset, q: params[:q]) }

投稿2019/08/20 08:04

編集2019/08/20 23:49
winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOO_

2019/08/21 16:21

お返事ありがとう御座います。細かくご教示いただきありがとう御座います。ただ、私の中で本当にスキル不足もあり、なかなか全てを理解するのが難しいかもしれません。offsetについては特に使ったこともなく更に混乱しそうなので、ご教示頂いた内容をゆくゆく導入して行きたいと思います。 その中で色々やってみたのですが、もしわがままを言って良ければ内容を拝見頂けないでしょうか?よろしくお願い致します。
KOO_

2019/08/21 16:26

ちょっとcontrollerは解決出来なさそうでした。
guest

0

リクエスト先のparams[:q]に対して元のparams[:q]を渡してやれば良いです。

rb

1link_to "前へ", customer_path(prev_customer, q: params[:q]), class: "prev btn btn-danger"

こういうことかな?よくわからないけど。

rb

1def prev_customer 2 @q.result.where("id < ?", id).last 3end

投稿2019/08/20 03:10

編集2019/08/20 08:26
Mugheart

総合スコア2344

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOO_

2019/08/20 03:38

ご回答頂き誠にありがとう御座います。上記でparams[:q]を渡しておりますが、結果が変わらないようです。 その他考えられることは御座いますでしょうか?よろしくお願い致します。
KOO_

2019/08/20 03:38

ご回答頂き誠にありがとう御座います。上記でparams[:q]を渡しておりますが、結果が変わらないようです。 その他考えられることは御座いますでしょうか?よろしくお願い致します。
Mugheart

2019/08/20 04:00

結果が変わらないというのは見た目の上だけの話ですか?それともコードをデバッグした上での話ですか? 結果が変わらないという表現だけでは当事者にしかわからないので何も考えられることがないです。 強いて挙げるなら回答自体が間違えていたか、ransackの使い方を間違えているかですかね。 ransackを使ったことがないのでもしかしたら間違えているのかもしれません。 現状あげられているコードのみで考えた場合showからshowへの移動の際 @q = Customer.ransack(params[:q]) の部分はparams[:q]が与えられないので @q = Customer.ransack(nil) となっています。なので上記の回答をしただけです。
KOO_

2019/08/20 04:49

ありがとう御座います。おそらくindexでは抽出されている条件がshowでは適応されていないのですね。 ちなみにindexでの条件をshowで引き継ぐ方法は御座いますでしょうか?お手数おかけしますが、よろしくお願い致します。
Mugheart

2019/08/20 04:56

indexでの条件というのは何のことでしょうか? 具体的にコードで示してもらえるとわかりやすいです。
KOO_

2019/08/21 16:08

ご連絡遅くなりました。 indexにあるsearch条件を追加しました。 よろしくお願い致します。
Mugheart

2019/08/22 01:57 編集

回答に追記した通り @q.result から前後のレコードを取得して移動すればいいのでは?と思ったんですが。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問