Home > Software design >  return nil unless collection.total_pages > 1
return nil unless collection.total_pages > 1

Time:03-11

I got this error and I have no idea how to fix it,

ActionView::Template::Error (undefined method `total_pages' for 
[#<Request id: 37, 
description: "food OK \r\nfood OK", 
title: "food", user_id: 1, category_id: 8, 
created_at: "2021-07-27 12:26:03.538236000  0700", 
updated_at: "2022-01-24 23:54:49.841988000  0700", 
slug: "food", 
status: 0>]:Array

      return nil unless collection.total_pages > 1

Can anyone advise how to resolve this issue?
Please find the below for your reference

app/controllers/api/search_autocomplete_controller.rb

module Api
  class SearchAutocompleteController < ApplicationController
    def index
      results = Elasticsearch::Model.search(params[:term], [..., Request])
      
      ...
      @requests  = results.select { |result| result["_type"] == 'request' }
    end
  end
end

/app/controllers/search_controller.rb

class SearchController < ApplicationController
    ...
    @request_records = Request.search(query_term, page: params[:page], per_page: 5)
    @requests = @request_records.select { |request| request.actived? }
    ...
end

/app/models/request.rb

class Request < ApplicationRecord
  ...
  # will_pagination configuration
  self.per_page = 10
  
  searchkick

  def search_data(options = {})
    self.as_json({
      only: [:title, :description, :updated_at, :slug],
      include: {
        user: { only: [:username] }
      }
    })
  end

  def actived?
    status == 0
  end

  ...
end

/app/views/search/show.html.erb

..
      <% if @requests.any? %>
        <div>
          <h4>Requests</h4>
          <%= render partial: 'search/request_card', object: @requests %>
        </div>
        <div >
          <%= will_paginate @requests %>
        </div>
      <% end %>
..

the issue is from <%= will_paginate @requests %>

gems
gem 'searchkick', '~> 5.0', '>= 5.0.2'
gem 'will_paginate', '~> 3.3', '>= 3.3.1'

CodePudding user response:

I can fix this issue by updating the script as below

<%= will_paginate @request_records %>
  • Related