small medium large xlarge

Generic-user-small
07 Jan 2012, 05:42
Kevin Young (2 posts)

Hello,

I have AJAX succesfully working, but unfortunately each time I click “Add to Cart” it duplicates the entry, so instead of the quantity being 1, it 2. Can anyone help me fix this problem?

This is the output of my log file after I click “Add to Cart”:

Started POST “/line_items?product_id=2” for 127.0.0.1 at 2012-01-07 00:38:00 -0500 Processing by LineItemsController#create as JS Parameters: {“authenticity_token”=>”DF4oiMAgDuaZeW/gyEHLuzJl/QP6DAYwtX98lq8XGlA=”, “product_id”=>”2”} Cart Load (0.3ms) SELECT carts.* FROM carts WHERE carts.id = 26 LIMIT 1 Product Load (0.3ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 LineItem Load (0.4ms) SELECT line_items.* FROM line_items WHERE line_items.cart_id = 26 AND line_items.product_id = 2 LIMIT 1 Product Load (0.3ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 (0.2ms) BEGIN (0.3ms) UPDATE line_items SET quantity = 7, updated_at = ‘2012-01-07 05:38:01’ WHERE line_items.id = 148 (0.4ms) COMMIT LineItem Load (0.4ms) SELECT line_items.* FROM line_items WHERE line_items.cart_id = 26 Product Load (0.3ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 Rendered line_items/_line_item.html.erb (2.1ms) Rendered carts/_cart.html.erb (5.2ms) Rendered line_items/create.js.erb (6.0ms) Completed 200 OK in 52ms (Views: 10.3ms | ActiveRecord: 10.7ms)

Started POST “/line_items?product_id=2” for 127.0.0.1 at 2012-01-07 00:38:01 -0500 Processing by LineItemsController#create as JS Parameters: {“authenticity_token”=>”DF4oiMAgDuaZeW/gyEHLuzJl/QP6DAYwtX98lq8XGlA=”, “product_id”=>”2”} Cart Load (0.2ms) SELECT carts.* FROM carts WHERE carts.id = 26 LIMIT 1 Product Load (0.3ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 LineItem Load (0.4ms) SELECT line_items.* FROM line_items WHERE line_items.cart_id = 26 AND line_items.product_id = 2 LIMIT 1 Product Load (0.3ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 (0.2ms) BEGIN (0.3ms) UPDATE line_items SET quantity = 8, updated_at = ‘2012-01-07 05:38:01’ WHERE line_items.id = 148 (0.3ms) COMMIT LineItem Load (0.4ms) SELECT line_items.* FROM line_items WHERE line_items.cart_id = 26 Product Load (0.4ms) SELECT products.* FROM products WHERE products.id = 2 ORDER BY title LIMIT 1 Rendered line_items/_line_item.html.erb (2.4ms) Rendered carts/_cart.html.erb (5.4ms) Rendered line_items/create.js.erb (6.3ms) Completed 200 OK in 82ms (Views: 10.7ms | ActiveRecord: 11.1ms)

For some reason it is creating two SQL queries. I tried removing the :remote => true from the button_to tag that creates the “Add to Cart” button, and it stopped making the duplicate entries, but of course it would be nice to have AJAX and not have to refresh the browser every time. So I am not sure if it has something to do with the :remote => true part of the code. I’ve looked over the forum and found someone else with this problem but no solution to it, so any help would be greatly appreciated.

Thanks,

Kevin

Generic-user-small
08 Jan 2012, 00:02
Kevin Young (2 posts)

For anyone having this problem, I ended up figuring it out. I had two sets of javascript files, one in my public/javascripts directory and one in my apps/assets/javascripts directory. Since rails 3.1 is designed to use the asset pipeline, I deleted my public/javascripts directory, and it started working just fine.

You must be logged in to comment