试图结合 will_paginate gem 和 pg_search gem

Trying to combine will_paginate gem and pg_search gem

我正在尝试添加分页器和搜索。它工作正常,直到我添加 pg_search。 pg_search 向右 URL 但不过滤搜索结果。例如,如果我键入 Vere Lee - http://localhost:3000/admin/dashboard?utf8=%E2%9C%93&search=Vera+Lee&commit= ,它会在不过滤的情况下停留在同一页面。如果我拿走分页器,pg_search 似乎工作正常。

有没有办法将它们结合起来?

控制器

   class AdminController < ApplicationController
      before_action :require_login
      def dashboard
      @users = User.joins(:filled_documents).group("users.id").having("count(filled_documents.id) > ?",0).order("created_at ASC").paginate(page: params[:page], per_page: 5).perform_search(params[:search])
    end

Views/dashboard

<header class="title">
  <h1>Dashboard</h1>
</header>

<form>
  <div class="grid-container">
    <div class="grid-x grid-padding-x">
      <div class="medium-6 cell">
        <label>Search Field
          <%= form_tag(@users_path, method: "get") do %>
          <%= text_field_tag :search, nil, placeholder: "Search either company, first or last name", class: "form-control" %>
          <%= submit_tag "", style: "display: none;" %>
            <% end %>
        </label>
      </div>
    </div>
  </div>
</form>

<%= will_paginate @users %>
<section class="wrapper">
  <div class="span-10">

    <% @users.each do |user| %>
      <h4><a style="color: #000;" href='/admin/usershowinfo?id=<%= "#{user.id}" %>'><%= "#{user.first_name} #{user.last_name} - #{user.company_name}" %></a></h4>
      <table class="table-minimal">
        <thead>
          <tr>
            <th colspan=4>Name</th>
            <th>State</th>
            <th colspan=3>Actions</th>
          </tr>
        </thead>

        <tbody>
          <% user.filled_documents.each do |filled_document| %>
          <tr>
            <td colspan=4>
              <%= "#{filled_document.name.empty? ? "Untitled" : filled_document.name}" %> <span class="badge-notice"><%= filled_document.comments.count %></span>
              <br>
              <i><%= filled_document.document.name %></i>
            </td>
            <td><%= filled_document.valid? ? "Complete" : "In Progress" %></td>
            <td colspan=3>
              <%= link_to 'View', document_filled_document_path(filled_document.document, filled_document), class: "btn" %>
              <%= link_to 'Edit', edit_document_filled_document_path(filled_document.document, filled_document) , class: "btn"%>
              <%= link_to 'Download', download_document_filled_document_path(filled_document.document, filled_document) , class: "btn" if filled_document.valid? %>
              <%= link_to 'Delete', [filled_document.document, filled_document], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn" %>
            </td>
          </tr>
          <% end %>
        </tbody>
      </table>
      <hr>
    <% end %>


  </div>
</section>
<%= will_paginate @users %>

用户模型

class User < ActiveRecord::Base
  include Clearance::User

  has_many :filled_documents

  include PgSearch
   pg_search_scope :search,
                   against: [
                     :company_name,
                     :first_name,
                     :last_name
                   ],
                   using: {
                     tsearch: {
                       prefix: true,
                       normalization: 2
                     }
                   }

  belongs_to :organisation

  validates_presence_of :first_name, :last_name, :company_name, :phone_number

  def self.perform_search(keyword)
    if keyword.present?
    then User.search(keyword)
  else User.all
    end
  end

  def update_password newpass
    puts "user.update_password called for #{first_name} #{last_name}"
    self.password = newpass
    save(validate: false)
  end

  def accept_new_tnc
    return false if admin?
    latest_signup_tnc = TermsAndCondition.signup.last
    return true if self.accepted_tnc != latest_signup_tnc.pdf_hash_value
    false
  end

  def update_accepted_tnc
    latest_signup_tnc = TermsAndCondition.signup.last
    self.accepted_tnc = latest_signup_tnc.pdf_hash_value
    self.accepted_tnc_date = DateTime.now
    self.save
  end

  def accept_legal_advice
    latest_legal_advice_tnc = TermsAndCondition.legal_advice.last
    self.accepted_legal_advice = latest_legal_advice_tnc.pdf_hash_value
    self.accepted_legal_advice_date = DateTime.now
    self.save
  end
end

为了避免加入和分组,您可以使用 belongs_to :user, counter_cache:true(还需要迁移以向用户 table 添加 filled_documents_count 整数字段并填充初始值)

此外,paginator 通常是链中的最后一个范围:

@users = User.where("filled_documents_count>?", 0)
@users = @users.perform_search(params[:search]) if params[:search].present?
@users = @users.order("created_at ASC").paginate(page: params[:page], per_page: 5)

但请注意,当多个并发写入同一用户的文档时,counter_cache 性能不佳。