试图结合will_paginate gem和pg_search gem

时间:2018-10-04 16:12:49

标签: ruby-on-rails pagination will-paginate pg-search

我正在尝试添加分页器和搜索。直到我添加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

视图/仪表板

<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

1 个答案:

答案 0 :(得分:0)

要解决加入和分组的问题,您可以使用belongs_to :user, counter_cache:true(还需要进行迁移以将filled_documents_count整数字段添加到用户表并填充初始值)

分页器通常也是链中的最后一个作用域:

@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在对同一用户的文档可以有多个并发写入时效果不佳。