简体   繁体   中英

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. 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.

To update only corresponding table you need to customize page parameter in at least one of the tables. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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