我正在努力让这个购物车应用程序发生,但由于某种原因,似乎POST请求的数量等于过去的GET请求的总和。这导致了一些令人讨厌的行为! 我无法弄清楚为什么......有人有想法吗?
的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 %>