javascript - Rails will_paginate 同一页面上的两个对象,从搜索字段传递一个参数

标签 javascript ruby-on-rails ruby

我对两个实例变量的结果使用 will_paginate,每个实例变量在两个不同的 PgSearch.multisearch 方法上使用相同的 param[:query]。在 View 中,当单击其中一个分页链接时,两个表都会被更新。有什么办法可以解决这个问题吗?仅传递表单中的一个参数? (因为我的搜索表单只有一个文本字段)。我搜索并浏览了类似的问题,其中大多数建议使用两个参数,但没有一个给我任何想法来使用一个参数解决这个问题。 :/

代码形式:

<%= form_tag("/search", :method => "get", :remote => true) do %>
  <%= label_tag(:query, "Search for: ") %>
  <%= text_field_tag(:query, params[:query]) %>
  <%= submit_tag("Search", class: "btn btn-primary") %>
<% end %>

Controller 代码:

def index
  @employee = Employee.find(session[:user_id])
  @search_employees = PgSearch.multisearch(params[:query]).where(:searchable_type => "Employee").paginate(page: params[:page], :per_page => 5)
  @search_customers = PgSearch.multisearch(params[:query]).where(:searchable_type => "Customer").paginate(page: params[:page], :per_page => 5)
  respond_to do |f|
    f.html
    f.js
  end
end

View 中的代码:

<% if !@search_employees.empty? %>
<h2>Employees</h2>
<table class="table table-hover">
.
.
<% @search_employees.each_with_index do |doc, index| %>
  <% user = doc.searchable %>
  <tr id="employee-<%= user.id %>">
  .
  .
<% end %>
  </tbody>
</table>
<% end %>
<%= will_paginate @search_employees %>

<% if !@search_customers.empty? %>
<h2>Customers</h2>
<table>
.
.
</table>
<% end %>
<%= will_paginate @search_customers %>

我可以从表单发送两个具有相同文本字段值的不同参数吗?如果是这样,请告诉我如何做。任何想法或建议将不胜感激。 :)

提前致谢。 :)

最佳答案

分页取决于 page 参数,但不取决于搜索表单中的 query 参数,因此您无需更改表单中的任何内容。

要仅更新相应的表,您需要在至少一个表中自定义 page 参数。例如更改客户的页面参数:

<%= will_paginate @search_customers, :param_name => 'customers_page' %>

在 Controller 中:

@search_customers = PgSearch.multisearch(params[:query])
  .where(:searchable_type => "Customer")
  .paginate(:page => params[:customers_page], :per_page => 5)

这应该可以解决问题

关于javascript - Rails will_paginate 同一页面上的两个对象,从搜索字段传递一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652887/

相关文章:

ruby-on-rails - Docker-compose 给出 URI::InvalidURIError:方案 postgresql 不接受注册表部分:$DB_USER:$DB_PASSWORD@$DB_HOST:port(或主机名错误?)

ruby-on-rails - Devise用户的个人资料模型?

javascript - AngularJS - 从 ng-repeat 中的 ng-select 中过滤已经选择的项目

javascript - 为什么链接需要在手机上双击才能进入链接?

ruby-on-rails - 按在 Postgresql 和 Ruby On Rails 中创建的顺序显示连接记录数据

ruby - 将 URL 和 @* 转换为链接

ruby - 为什么 Ruby 的标准库有 DateTime 类?

ruby - 有没有更简单的方法使用 Ruby 将时间戳插入文件名?

javascript - 选择自动完成中的第一项时,jQuery mouseleave 会触发

javascript - jQuery:为什么我的动画 p "jump"?