繁体   English   中英

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

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

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

代码形式如下:

<%= 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 %>

控制器代码:

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

视图中的代码:

<% 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' %>

在控制器中:

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

这应该解决问题

暂无
暂无

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

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