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

On Tue, Jul 7, 2015 at 11:08 AM, Faizan A. [email protected]
wrote:

     $.ajax({


.
For more options, visit https://groups.google.com/d/optout.


Mobile : +91 8978 016 276

Facebook : http://www.facebook.com/bala.kishore.21
linkedin : http://www.linkedin.com/profile/view?id=7562848
twitter : https://twitter.com/balakishorep

On 7 July 2015 at 06:38, Faizan A. [email protected] wrote:

     $.ajax({
            type: "POST",
            url: "/students/",
            data: dataString,
            success: function() {
              window.alert('success');
              }
          });
});

});

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

Bala kishore Pulicherla wrote in post #1176274:

try with type: “PUT”

“POST” is for create

On Tue, Jul 7, 2015 at 11:08 AM, Faizan A. [email protected]
wrote:

     $.ajax({


.
For more options, visit https://groups.google.com/d/optout.


Mobile : +91 8978 016 276

Facebook : http://www.facebook.com/bala.kishore.21
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 L. wrote in post #1176283:

On 7 July 2015 at 06:38, Faizan A. [email protected] wrote:

     $.ajax({
            type: "POST",
            url: "/students/",
            data: dataString,
            success: function() {
              window.alert('success');
              }
          });
});

});

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

Yes, may be you’re right, I simply want to perform “edit” action with
Ajax, but it let’s me do “create” action instead.

On Tue, Jul 7, 2015 at 1:38 AM, Faizan A. [email protected] wrote:

     $.ajax({
            type: "POST",
            url: "/students/",

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


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

Dave A., 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”;

Dave A. wrote in post #1176300:

On Tue, Jul 7, 2015 at 1:38 AM, Faizan A. [email protected] wrote:

     $.ajax({
            type: "POST",
            url: "/students/",

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


Dave A., 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.

On 7 July 2015 at 15:11, Faizan A. [email protected] wrote:


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.

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

Colin

On Jul 7, 2015, at 8:14 AM, Faizan A. [email protected] wrote:

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.

First, what’s set up in your routes?

Second, what does the controller dispatched to actually do?

On Tue, Jul 7, 2015 at 10:14 AM, Faizan A. wrote:

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.

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


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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs