jeudi 9 avril 2015

Ruby on Rails - POST requests too many times

I have a problem with my shopping cart app: when I press the add-to-cart button, the POST request is submitted multiple times. For some reason, it is usually rising with a multiple of three. For example, the first time the POST request is sent 4 times, after refreshing 7 times, 11 times, ...


I honestly have no idea where to look for the error, but here is some code:


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 index:



<% @products.each do |product| %>
<%= render "product_row", product: product %>
<% end %>


=> Partial 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 %>

Aucun commentaire:

Enregistrer un commentaire