ruby on rails - Updating Cart content based on option selected -
while implementing cart, want allow user change quantity of item has in cart. right now, have select tag display quantity, correct quantity pre-selected. want this: user selects option in select tag, quantity in cart updated. how should proceed? right quantity displayed follows:
<%= form_tag %> <%= select_tag "quantity", options_for_select((1..item.quantity).to_a, quantity) %> <% end %> one more thing. show_cart partial doesn't include checkout button, if that's of help.
this can achieve through ajax. need execute jquery whenever user selects value of <select> tag.
let's select_tag generate following <select> tag in html:
<select id="quantityoptions"> <option value="1">one item</option> <option value="2">two items</option> <option value="3">three items</option> <option value="4">four items</option> </select> in javascript file, need .change() event, , write ajax call in it.
$("#quantityoptions").change(function() { var currentquantity = $(this).val(); // give currenlty selected option in <select> tag $.ajax( method: "post", url: http://localhost:3000/update_quantity, // or whatever custom url have built data: { val: currentquantity }, success: function() { console.log("the value reached on server"); } ); }); sure, refine more; idea of it. , don't forget have authentication on server, may need attach request token each request.
Comments
Post a Comment