简体   繁体   English

Rails会在同一页面上抓住两个对象,从搜索字段传递一个参数

[英]Rails will_paginate two objects on the same page, passing one param from search field

I am using will_paginate on the results from two instance variables, each using the same param[:query] on two different PgSearch.multisearch methods. 我在两个实例变量的结果上使用了will_paginate,每个变量在两个不同的PgSearch.multisearch方法上使用相同的param[:query] In the views, when clicking on one of the pagination links, both the tables were being updated. 在视图中,当单击其中一个分页链接时,两个表都在更新。 Is there any way to get around this problem? 有没有办法解决这个问题? By passing only one param from the form? 从表单中只传递一个参数? (As my search form has only one text field). (因为我的搜索表单只有一个文本字段)。 I have searched and gone through the similar questions, most of them suggesting to use two params., but none of them gave me any thoughts to solve this prob using one param. 我搜索并经历了类似的问题,其中大多数建议使用两个参数。但是他们都没有给我任何想法用一个参数来解决这个问题。 :/ :/

Code in the form: 代码形式如下:

<%= 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 code: 控制器代码:

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

Code in the 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 %>

Can I send two different params with the same text field value from the form? 我可以从表单中发送两个具有相同文本字段值的不同参数吗? if so., please let me know how. 如果是的话,请让我知道如何。 Any ideas or suggestions would be much appreciated. 任何想法或建议将不胜感激。 :) :)

Thanks in Advance. 提前致谢。 :) :)

Pagination depends on the page parameter, but not on the query parameter from the search form, therefore you don't need to change anything in your form. 分页取决于page参数,但不取决于搜索表单中的query参数,因此您无需更改表单中的任何内容。

To update only corresponding table you need to customize page parameter in at least one of the tables. 要仅更新相应的表,您需要在至少一个表中自定义page参数。 For example change page parameter for customers: 例如,为客户更改页面参数:

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

and in controller: 在控制器中:

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

This should resolve the issue 这应该解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM