如何将多个表单提交到单个查询字符串

How to submit multiple forms to a single query string

我有以下 rails 索引页

search/index.haml

= form_tag search_path, method: 'get' do
  = text_field_tag :q, params[:q], placeholder: 'Search...'
  = submit_tag 'search'

= link_to 'testing', search_path(params.except(:controller, :action).merge(p: 'testing'))

输入值 "product" 并提交表单给我这个查询字符串

http://localhost:3000/search?utf8=✓&q=product

提交表单后单击 link 会给我

http://localhost:3000/search?p=testing&q=product&utf8=✓

有没有一种方法可以添加另一个表单并将其提交给现有的查询字符串,就像 link 的行为方式一样?

我们的想法是使用第二种形式作为搜索的一部分 aggregations/facets。

只需像为 link 创建 URL 一样创建 URL:

form_tag search_path(params.except...)

...并将您的字段名称从 q 更改为 q[],这样 Rails 就会明白您正在发送一个值数组。

然后在服务器端,您需要将它们重新连接起来,例如:query = params[:q].join " "