Skip to content

Instantly share code, notes, and snippets.

@yurifrl
Created March 12, 2014 12:45
Show Gist options
  • Save yurifrl/9506209 to your computer and use it in GitHub Desktop.
Save yurifrl/9506209 to your computer and use it in GitHub Desktop.
ajax update rails
var ids = undefined;
$(document).ready(function () {
var $form = $("form.edit_product");
var form_action = $form.attr("action");
var product_name_id = form_action.split('/')[3];
var option_types_url = window.location.protocol + "//" + window.location.host + "/" + 'api/products/' + product_name_id;
$form.submit(function(e, data){
e.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
url: form_action,
method: "PUT",
data: {product: {option_type_ids: $("#product_option_type_ids").val()}, utf8: "✓", authenticity_token: encodeURIComponent($('#authenticity-token').val()), variant_page: "true"}
}).fail(function(data){
alert("FAIL");
}).success(function(data){
console.log(data);
$.getJSON(option_types_url, null ).done(function(json) {
$.each(json.option_types, function(index, value) {
console.log(value.id);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment