Trung Tran
Trung Tran

Reputation: 13721

How to edit data with AJAX?

How do I edit an object with a POST request?

I built a model for Recipe using Rails. Its attributes include recipe_name, instructions, time_of_day, etc.

I was able to figure out how to pull data using $.getJSON but I'm not sure how to edit an attribute.

So far I tried to look at the jQuery POST documentation but it didn't really address my question. Can someone help?

One thing I have tried is adding a second button to update information:

       $("#button2").click(function() {

    $.post('http://localhost:3000/recipes/1',

    {
      recipe_name: 'pot roast' //this is supposed to update the recipe name
      author: 'Tin Nguyen' //this is supposed to update the author name
    })

    alert('success!')
  })


})

This did not work for me.

Upvotes: 0

Views: 59

Answers (1)

Arthur Frankel
Arthur Frankel

Reputation: 4705

Not sure if you wanted a call back from your post, but this is how you do it. And you were missing a comma between the attributes you were passing.

$("#button2").click(function() {

    $.post('http://localhost:3000/recipes/1',

    {
        recipe_name: 'pot roast', //this is supposed to update the recipe name
        author: 'Tin Nguyen' //this is supposed to update the author name
    }, function( data ) {
       alert("success");
    }
});

Upvotes: 1

Related Questions