Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 3 form_标记方法::仅包含已更改的字段_Ruby On Rails_Forms_Activeview - Fatal编程技术网

Ruby on rails rails 3 form_标记方法::仅包含已更改的字段

Ruby on rails rails 3 form_标记方法::仅包含已更改的字段,ruby-on-rails,forms,activeview,Ruby On Rails,Forms,Activeview,我正在编写一个搜索表单,它将重新加载页面,并将搜索参数添加到URL中 当前,我的表单生成的URL包含所有表单字段,包括空字段。这会使生成的url中经常不需要的数据变得杂乱无章 我希望我的表单重定向到一个url,该url只包含包含数据的字段 这是我表格的代码: = form_tag orders_path, method: :get do = text_field_tag :search, params[:search], class: "span2 appendedInputButton

我正在编写一个搜索表单,它将重新加载页面,并将搜索参数添加到URL中

当前,我的表单生成的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
当我点击提交按钮而不填写任何表单时,我会被重定向到如下url:

http://localhost:3000/orders?utf8=%E2%9C%93&search=&order_types%5B%5D=
http://localhost:3000/orders
我希望url是这样的:

http://localhost:3000/orders?utf8=%E2%9C%93&search=&order_types%5B%5D=
http://localhost:3000/orders
如果我只选择一些
订单类型
,我会得到:

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
或者类似但更简洁的东西


非常感谢您的帮助,

form\u for
将表单绑定到一个对象,rails内部将使用前缀命名与该对象(或多个对象)相关的字段的名称空间,以便在提交表单时,数据将以
params[:prefix][:some_data]
的形式显示在参数中


为了减小查询字符串的大小,我建议您使用
form\u标签
和。通过这种方式,您可以完全控制提交的字段,还可以避免
utf8=%E2%9C%93
utf8强制雪人攻击。

谢谢,
form\u tag
实际上是我使用的帮助器,并且表单必须包含我要为其选择的属性的所有字段。我不确定的是如何减少生成的url的大小。请尝试使用
=form\u tag orders\u path,method::get,exforce\u utf8:false do
,这将删除utf8强制。要从url中删除
search=
,您必须在提交之前使用javascript从表单中排除/禁用(使用html元素的
disabled='disabled'
属性),否则它将始终以查询字符串的形式发送。感谢您的回复,
enforce\u utf8:false对我不起作用,
被禁用
的想法我不确定我是否理解,您是否建议ajax在按下提交按钮后动态添加属性?
强制执行utf8:false
已在Rails 4->中引入。关于“disabled”属性-是的,您必须让javascript来执行此操作。