Rails 4 - Ajax form adding a new record instead of editing the current record

I have a model let's say Student and I want to edit a row in the table name students through Ajax after clicking a button, what is the correct way to do this? Also, please note I want to edit value inside one column followers

Here is the js code:

    $(document).ready(function(){     $('.clap').click(function(){         var dataString = "student[followers]=5";          $.ajax({                 type: "POST",                 url: "/students/",                 data: dataString,                 success: function() {                   window.alert('success');                   }               });     }); });

try with type: “PUT”

“POST” is for create

Not sure what you are asking. Are you asking how to perform the action in the controller when it receives the POST? If not then please try to explain again exactly what the question is. Also say what you have already tried.

Colin

First, as bala kishore pulicherla said, you should be using PUT rather than POST. Second, though, you also need to tell Rails *which* student to update. So, rather than /students/, it should be /students/42 or whatever.

-Dave

Bala kishore Pulicherla wrote in post #1176274:

try with type: "PUT"

"POST" is for create

         $.ajax({ -- . For more options, visit https://groups.google.com/d/optout.

-- Mobile : +91 8978 016 276

Facebook : Bala Kishore linkedin : http://www.linkedin.com/profile/view?id=7562848 twitter : https://twitter.com/balakishorep

$(document).ready(function(){     $('.clap').click(function(){         var dataString = "student[followers]=5";          $.ajax({                 type: "POST",                 url: "/students/",                 data: dataString,                 success: function() {                   window.alert('success');                   }               });     }); });

I tried with URL "/students/1", which is obviously for editing the Student with id 1, but with type: "POST" the URL returns 404 error in the console. So, I tried with type: "PUT", the URL here doesn't return the 404 error but it doesn't update the Student.

Colin Law wrote in post #1176283:

Dave Aronson wrote in post #1176300:

Dave Aronson, consulting software developer of Codosaur.us, PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

I tried with URL "/students/1", which is obviously for editing the Student with id 1, but with type: "POST" the URL returns 404 error in the console. So, I tried with type: "PUT", the URL here doesn't return the 404 error but it doesn't update the Student.

Also, I have double checked the field I'm gonna update i.e, var dataString = "student[followers]=5";

First, what's set up in your routes?

Second, what does the controller dispatched to actually do?

Look in log/development.log to get more information about what is happening.

Colin

Respectful of all that have offered here, I would like to ask for a little more information. Please post here your View (…html.erb) , the relevant view that contains class clap, and the controller underlying that view. And then run rake routes, and provide that result. Thanks, Liz

Are you using the stock-standard generator-generated routes and controller? Can you "puts" something in the controller's update action to confirm that you are indeed reaching the correct action? That would solve as much of it as we can with the information already given. If you are reaching the right controller action but the desired updating is not happening, we'll need to see the code in the controller action.

-Dave