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

40 views
Skip to first unread message

Faizan Ali

unread,
Jul 7, 2015, 1:39:40 AM7/7/15
to rubyonra...@googlegroups.com
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');
}
});
});
});

--
Posted via http://www.ruby-forum.com/.

bala kishore pulicherla

unread,
Jul 7, 2015, 1:43:09 AM7/7/15
to rubyonra...@googlegroups.com
try with type: "PUT"

"POST" is for create

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d3c0ecb93ba57b861a36ec94b997f184%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.



--

Colin Law

unread,
Jul 7, 2015, 4:00:32 AM7/7/15
to rubyonra...@googlegroups.com
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

Dave Aronson

unread,
Jul 7, 2015, 6:55:15 AM7/7/15
to rubyonrails-talk
On Tue, Jul 7, 2015 at 1:38 AM, Faizan Ali <li...@ruby-forum.com> 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 Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

Faizan Ali

unread,
Jul 7, 2015, 10:11:46 AM7/7/15
to rubyonra...@googlegroups.com
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 Ali <li...@ruby-forum.com>
> 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.

Faizan Ali

unread,
Jul 7, 2015, 10:13:41 AM7/7/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1176283:
Yes, may be you're right, I simply want to perform "edit" action with
Ajax, but it let's me do "create" action instead.

Faizan Ali

unread,
Jul 7, 2015, 10:14:45 AM7/7/15
to rubyonra...@googlegroups.com
Dave Aronson wrote in post #1176300:
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.

Faizan Ali

unread,
Jul 7, 2015, 10:17:10 AM7/7/15
to rubyonra...@googlegroups.com
>> 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";

Scott Ribe

unread,
Jul 7, 2015, 10:29:44 AM7/7/15
to rubyonra...@googlegroups.com, Faizan Ali
On Jul 7, 2015, at 8:14 AM, Faizan Ali <li...@ruby-forum.com> 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?

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Colin Law

unread,
Jul 7, 2015, 11:38:15 AM7/7/15
to rubyonra...@googlegroups.com
On 7 July 2015 at 15:11, Faizan Ali <li...@ruby-forum.com> 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

Elizabeth McGurty

unread,
Jul 7, 2015, 11:41:34 AM7/7/15
to rubyonra...@googlegroups.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

Dave Aronson

unread,
Jul 7, 2015, 12:10:14 PM7/7/15
to rubyonrails-talk
On Tue, Jul 7, 2015 at 10:14 AM, Faizan Ali 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.
Reply all
Reply to author
Forward
0 new messages