ruby-on-rails - 试图结合 will_paginate gem 和 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
视图/仪表板
<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
(还需要迁移以将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
当同一用户的文档可以有多个并发写入时,效果不佳。
推荐阅读
- python - 从excel复制列
- vba - 公共子系统上的 MS Access Openform
- visual-sourcesafe - 在 VSS 6.0 中比较两个文件时文本不可见
- aws-lambda - 调用 AWS Lambda 的代理背后的 WSO2 Api 网关
- c# - C# 在自身内部启动线程
- apache - 带有哈希“#”字符的 apache 2.4 proxypass url
- asp.net-core - 在浏览器窗口中将二进制字符串作为纯文本返回(而不是作为可下载文件)
- python - psycopg2:复制与执行获取
- powershell - 如何获取仅今天日期的多个文件的子项
- python - 根据具有不同行数的另一个数据框列向熊猫数据框添加新列