【发布时间】:2013-10-27 14:22:21
【问题描述】:
我正在编写一个搜索表单,它将重新加载页面,并将搜索参数添加到 URL。
目前,我的表单生成的 URL 包含所有表单字段,包括空字段。这会使使用通常不需要的数据生成的 url 变得很混乱。
我希望我的表单重定向到仅包含包含数据的字段的 url。
这是我的表单的代码:
= form_tag orders_path, method: :get do
= text_field_tag :search, params[:search], class: "span2 appendedInputButtons"
= select_tag "order_types",
options_from_collection_for_select(OrderType.all, :id, :name),
multiple: true,
size: 8,
include_blank: "select"
%button.btn.btn-primary Update list
当我点击提交按钮而不填写任何表单时,我会被重定向到这样的网址:
http://localhost:3000/orders?utf8=%E2%9C%93&search=&order_types%5B%5D=
虽然我希望网址是这样的:
http://localhost:3000/orders
如果我只选择order_types 中的一部分,我会得到:
http://localhost:3000/orders?utf8=%E2%9C%93&search=&order_types%5B%5D=3&order_types%5B%5D=6
虽然我希望它是这样的:
http://localhost:3000/orders?order_types%5B%5D=3&order_types%5B%5D=6
甚至更好:
http://localhost:3000/orders?order_types=3,6
或类似的东西,但更简洁,
非常感谢您的帮助,
【问题讨论】:
标签: ruby-on-rails forms activeview