Rails远程表单提交重复的表单数据

时间:2016-01-21 08:38:36

标签: ruby-on-rails ajax forms ruby-on-rails-4

当我在rails中提交远程表单时,表单POST重复数据,即使我使用jQuery手动序列化表单也显示没有重复项。 可能导致这种情况的原因是什么?

*通常这不会有问题,但其中一个表单数据是一个数组,因此提交的数组中有重复的值。

来自chrome网络检查员的结果 enter image description here

来自jQuery的结果 Manual

更新 这是我的表单的gist(删除html标记):

<% @order.available_payment_methods.each do |method| %>
    <%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == @order.available_payment_methods.first %>
    <%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
    <%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>

# partial 

  <% param_prefix = "payment_source[#{payment_method.id}]" %>

    <%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
    <%= text_field_tag "#{param_prefix}[name]", "#{@order.billing_firstname} #{@order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>

    <% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
    <%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>


    <%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>

    <%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>


  <% if @order.bill_address %>
    <%= fields_for "#{param_prefix}[address_attributes]", @order.bill_address do |f| %>
      <%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
    <% end %>
  <% end %>
  <%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>

更新提交没有remote: true的表单可以解决问题(但我需要它!)

1 个答案:

答案 0 :(得分:0)

在表单上添加onsubmit="return false();",确保通过默认表单提交不提交表单。当您添加remote: true

时,它将通过ajax提交