表格提交两次,原因是:remote => true

我的表单提交了两次,经过双重检查,它是由’:remote => true’引起的。 我删除它,我的项目运作良好。 谁能告诉我为什么? 以及如何使用’:remote => true’?

我的ruby代码:

 true, :id => 'new_product_group_form') do %>  [:product_scopes, :groups, group_name]), scopes.keys.map do |scope_name| [ t(:name, :scope => [:product_scopes, :scopes, scope_name]), scope_name] end ] end ) %> 

<input type="submit" value="" />

浏览器中的最终html代码。

  

In Taxon(without descendants) In taxons and all their descendantsProduct name have following Product name or meta keywords have following Product name or description have following Products with IDsWith value With property With property value With option With option and valuePrice between Master price lesser or equal to Master price greater or equal to

如果人们像我一样绊倒这个问题:

我有同样的问题,并且sannankhalid的答案没有解决它,但删除了public/assets目录中的本地预编译的application.js – ujs被包含两次,因此它会触发两次。 通过https://stackoverflow.com/a/9627690/604093

在Rails 5上, rails-ujs取代了jquery_ujs 。 如果两者都需要,事件将触发两次。

 // app/assets/javascripts/application.js //= require jquery_ujs <-- delete this //= require rails-ujs 

我假设你正在使用jquery。 这通常发生在呼叫不完整或存在某种错误并且您没有刷新页面时。 尝试这样的事情:

  

似乎Ryan Muller的回答是正确的。 但是根据我的观点,删除application.js并不是正确的方法。 我所做的是我用chrome打开了开发人员的工具,然后点击网络部分。 现在当我点击提交按钮时,它会告诉我谁在提出请求。 所以我删除了JS并再次尝试它并且它有效。 因此,根据Ryan Muller的说法,正确认为JS的问题包括两次。 但请确保您也保持JS的依赖性。

在此处输入图像描述

尝试在服务器上使用ctrl-c来停止它。 然后rm -r public/assets/摆脱assets目录(和重复的application.js)。 从同一个终端窗口重新启动服务器,它可能会按预期工作。

想要为此添加另一个可能的原因。 对我来说,它使用的是Mixpanel的api。 具体来说, 请访问https://mixpanel.com/docs/integration-libraries/javascript-full-api#track_forms

看来,使用:remote=> truemixpanel.track_forms会导致表单在所需的json之后通过普通的html提交。

这可能是罕见的,但我花了一段时间追查。

这是HAML相当于sannankhalid的。

 :javascript = f.submit(function() { $(this).unbind('submit').submit(); }); 

我有Rails 4和Bootstrap 3(w / jQuery)的双POST问题,从模态提交表单更新。

在您的应用程序模板中(或者保留<%= javascript_include_tag“应用程序”%>标签或haml等效项的地方,添加“data-turbolinks-track”=> true标志,因此标记现在将如下所示:<% = javascript_include_tag“application”,“data-turbolinks-track”=> true%>。

一旦解决方案是移除谷歌标签管理器,它就发生在我身上,因为我正在跟踪表单提交。