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

iva*_*Lee 20 ruby forms post ruby-on-rails ruby-on-rails-3

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

我的红宝石代码:

<%= form_tag(admin_product_group_product_scopes_path(@product_group), :remote => true, :id => 'new_product_group_form') do %>
    <%
    options =
    grouped_options_for_select(
    Scopes::Product::SCOPES.map do |group_name, scopes|
      [
        t(:name, :scope => [:product_scopes, :groups, group_name]),
        scopes.keys.map do |scope_name|
          [ t(:name, :scope => [:product_scopes, :scopes, scope_name]), scope_name]
        end
      ]
    end
    )
    %>
    <p>
      <label for="product_scope_name"><%= t('add_scope') %></label>
      <%= select_tag("product_scope[name]", options) %>
      <input type="submit" value="<%= t('add') %>" />
    </p>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

浏览器中的最终html代码.

 <form accept-charset="UTF-8" action="/admin/product_groups/17/product_scopes" data-remote="true" id="new_product_group_form" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="?"><input name="authenticity_token" type="hidden" value="GocX/l4ZNgF/feKtzC8FuohebM2k5MuIHtdeGp2Oi0A="></div>
    <p>
      <label for="product_scope_name">Add a scope</label>
      <select id="product_scope_name" name="product_scope[name]"><optgroup label="Taxon"><option value="taxons_name_eq">In Taxon(without descendants)</option>
<option value="in_taxons">In taxons and all their descendants</option></optgroup><optgroup label="Text search"><option value="in_name">Product name have following</option>
<option value="in_name_or_keywords">Product name or meta keywords have following</option>
<option value="in_name_or_description">Product name or description have following</option>
<option value="with_ids">Products with IDs</option></optgroup><optgroup label="Values"><option value="with">With value</option>
<option value="with_property">With property</option>
<option value="with_property_value">With property value</option>
<option value="with_option">With option</option>
<option value="with_option_value">With option and value</option></optgroup><optgroup label="Price"><option value="price_between">Price between</option>
<option value="master_price_lte">Master price lesser or equal to</option>
<option value="master_price_gte">Master price greater or equal to</option></optgroup></select>
      <input type="submit" value="Add">
    </p>
</form>
Run Code Online (Sandbox Code Playgroud)

小智 52

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

我有同样的问题,sannankhalid的答案没有解决它,但删除public/assets目录中的本地预编译的application.js - ujs包含两次,所以它会触发两次.通过/sf/answers/673938331/

  • 修复了我的问题 - 推荐这个问题超过@ Muhammad的解决方案,因为它不是"黑客",而是实际解决问题的原因. (2认同)

Joã*_*uza 22

在Rails 5上,rails-ujs替换jquery_ujs.如果两者都需要,事件将触发两次.

// app/assets/javascripts/application.js
//= require jquery_ujs <-- delete this
//= require rails-ujs
Run Code Online (Sandbox Code Playgroud)