我的购物车应用程序出现问题:当我按下“添加到购物车”按钮时,POST请求会多次提交。出于某种原因,它通常以三倍的倍数上升。例如,第一次发送POST请求4次,刷新7次,11次后,......
老实说,我不知道在哪里寻找错误,但这里有一些代码:
的ApplicationController:
before_filter :current_delivery
def current_delivery
if session[:delivery_id]
@delivery = Delivery.find(session[:delivery_id])
else
@delivery = Delivery.create
session[:delivery_id] = @delivery.id
end
end
OrderItemsController:
def create
@delivery.order_items.new(order_item_params)
@delivery.save
end
的ProductsController:
def index
@products = Product.all
@order_item = OrderItem.new
end
产品索引:
<% @products.each do |product| %>
<%= render "product_row", product: product %>
<% end %>
=&GT;部分product_row:
<%= form_for @order_item, remote: true do |f| %>
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<%= f.hidden_field :product_id, value: product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
<% end %>