great084 / twitter_tool

ツイッターツールの開発
0 stars 0 forks source link

ページネーション したページ先でransack が動かない #43

Closed nakamitsu-nozomi closed 3 years ago

nakamitsu-nozomi commented 3 years ago

バグの現象

ページネーション で2ページ以降に移動した後に、検索機能が動かない

本来あるべき動作

ページネーション で2ページ以降に移動した後でも、検索できる

実際の動作

1ページ目であれば、検索できるが、2ページ目以降に移動した後に、検索機能が動かない

バグの原因

ページネーションを使った後に、 URL がhttp://localhost:3000/tweets?page=2のように?page=が追加された後に、ransack が動いてないことかなと考えました。

バグの解消方法

https://teratail.com/questions/130562 thrbolinksの設定をoffにするという回答記事もあるが正解なのでしょうか。

ページネーションを使いURLが変わった際の処理を別に記載する必要があるのでしょうか。それとも根本的に、ransackの使用方法が違うのでしょうか。

nakamitsu-nozomi commented 3 years ago

追記

application_contoroller.rbに下記のように記載してみたのですが、うまく機能しませんでした。

class ApplicationController < ActionController::Base
  include SessionsHelper
  before_action :set_tweet_search_query
  PER_PAGE = 10

  def set_tweet_search_query
    @user = current_user
    @q = Tweet.where(user_id: @user.id).ransack(params[:q])
    @tweets = @q.result(distinct: true).order(tweet_created_at: :desc).includes(:media).page(params[:page]).per(PER_PAGE)
    @now = Time.current
  end
end
great084 commented 3 years ago

これかもしれない。試してみてください。 https://qiita.com/hc_isobe/items/cfe1f31319780e73cebe

nakamitsu-nozomi commented 3 years ago

上記記事を参考にすることで、解決しました。ありがとうございました。