title: Ruby on rails - Kaminari - Paginate array of search results
body: <p>I have a search form with pagination. Both things work OK separately, but if I search and then load the next page, the search parameters are forgotten and kaminari shows results regardless of what search options I selected. </p>
  
  <p>I have read through the docs, and other stackoverflow posts but I'm not having any luck.</p>
  
  <p>My index page has </p>
  
  <pre><code>&lt;%= search_form_for @q, html: { id: "gig_search_form" }, remote: true do |f| %&gt;
  bla bla bla...
  &lt;% end %&gt;
  &lt;%= link_to_next_page @gigs, 'Next Page', :remote =&gt; true %&gt;
  </code></pre>
  
  <p>index.js.erb </p>
  
  <pre><code>&lt;% if params[:page] %&gt; 
   $('.gig-search-list').append("&lt;%= j render(partial: 'gigs') %&gt;");
  &lt;% else %&gt;
   $('.gig-search-list').html("&lt;%= j render(partial: 'gigs') %&gt;");
  &lt;% end %&gt;
  </code></pre>
  
  <p>gigs partial:</p>
  
  <pre><code>    &lt;% @gigs.each do |gig| %&gt;
        bla bla bla... 
      &lt;% end %&gt;
  </code></pre>
  
  <hr>
  
  <p>Now, I have tried a few tings in my controller but I'm always gettings the same results. Originally I had;</p>
  
  <pre><code>def index
        if params[:search].present?
          @q = Gig.notexpired.where(:filled =&gt; false).near(params[:search], 500, :order =&gt; 'distance' ).ransack(params[:q])
        else
          @q = Gig.notexpired.where(:filled =&gt; false).ransack(params[:q])
        end
          @allgigs = @q.result(distinct: true)
          @gigs = @q.result(distinct: true).page(params[:page]).per(5)
        respond_to do |format|
        format.js
        format.html
      end
    end
  </code></pre>
  
  <p>Then;</p>
  
  <pre><code>def index
        if params[:search].present?
          @q = Gig.notexpired.where(:filled =&gt; false).near(params[:search], 500, :order =&gt; 'distance' ).ransack(params[:q])
        else
          @q = Gig.notexpired.where(:filled =&gt; false).ransack(params[:q])
        end
          @allgigs = @q.result(distinct: true)
          @gigs = Kaminari.paginate_array(@allgigs).page(params[:page]).per(5)
        respond_to do |format|
        format.js
        format.html
      end
    end
  </code></pre>
  
  <p>Then I tried to separate them;</p>
  
  <pre><code>def index
        if params[:search].present?
          @q = Gig.notexpired.where(:filled =&gt; false).near(params[:search], 500, :order =&gt; 'distance' ).ransack(params[:q])
        else
          @q = Gig.notexpired.where(:filled =&gt; false).ransack(params[:q])
        end
        @allgigs = @q.result(distinct: true)
        if @allgigs.present?
          unless @allgigs.kind_of?(Array)
            @gigs = @allgigs.page(params[:page]).per(5)
          else
            @gigs = Kaminari.paginate_array(@allgigs).page(params[:page]).per(5)
          end
        end
        respond_to do |format|
        format.js
        format.html
      end
    end
  </code></pre>
  
  <p>But I get the same result each time. I can see on the console that when I load more pages, the search parameters are not included. </p>
  
  <p>How can I link the two things together and paginate the search results correctly?</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>Server log after loading 'next page' using @chaitanya's answer. I have previously added search params.</p>
  
  <pre><code>Started GET "/gigs?locale=en&amp;page=2" for 127.0.0.1 at 2016-06-29 14:45:53 +0200
  Processing by GigsController#index as JS
    Parameters: {"locale"=&gt;"en", "page"=&gt;"2"}
    Gig Load (10.3ms)  SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1  [["filled", "f"]]
    Gig Load (1.2ms)  SELECT  DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 LIMIT 5 OFFSET 5  [["filled", "f"]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 7]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 6]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 8]]
    Rendered gigs/_gigs.html.erb (13.4ms)
     (0.5ms)  SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1  [["filled", "f"]]
    Rendered gigs/index.js.erb (17.2ms)
  Completed 200 OK in 54ms (Views: 21.4ms | ActiveRecord: 13.0ms)
  </code></pre>
  
  <p>Server log when filtering with search form:</p>
  
  <pre><code>Started GET "/gigs?locale=en&amp;utf8=%E2%9C%93&amp;search=&amp;q%5Bdate_gteq%5D=&amp;q%5Bdate_lteq%5D=&amp;q%5Bgenres_id_in%5D%5B%5D=&amp;q%5Bsalary_cents_gteq_euros%5D=0.00&amp;q%5Bsalary_cents_lteq_euros%5D=210.00" for 127.0.0.1 at 2016-06-29 14:45:47 +0200
  Processing by GigsController#index as JS
    Parameters: {"locale"=&gt;"en", "utf8"=&gt;"✓", "search"=&gt;"", "q"=&gt;{"date_gteq"=&gt;"", "date_lteq"=&gt;"", "genres_id_in"=&gt;[""], "salary_cents_gteq_euros"=&gt;"0.00", "salary_cents_lteq_euros"=&gt;"210.00"}}
    Gig Load (0.6ms)  SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" &gt;= 0 AND "gigs"."salary_cents" &lt;= 21000)  [["filled", "f"]]
    Gig Load (0.5ms)  SELECT  DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" &gt;= 0 AND "gigs"."salary_cents" &lt;= 21000) LIMIT 5 OFFSET 0  [["filled", "f"]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 8]]
    CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 7]]
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
    Rendered gigs/_gigs.html.erb (12.2ms)
     (0.4ms)  SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime &gt; '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" &gt;= 0 AND "gigs"."salary_cents" &lt;= 21000)  [["filled", "f"]]
    Rendered gigs/index.js.erb (15.6ms)
  Completed 200 OK in 33ms (Views: 19.9ms | ActiveRecord: 5.7ms)
  </code></pre>