I am attempting to do an inline (i.e. not using a form and not going to another page) update (and, separately but same problem, delete) of a record.
I am using ruby, sinatra and SEQUEL with postgres.
The jquery script is as follows:
<script>
$(".update").click(function() {
var id = $(this).attr('id');
alert(id);
var email = $('#email'+id).val();
var user = $('#user'+id).val();
var pass = $('#pass'+id).val();
alert(email + ' ' + user + ' ' + pass)
$.ajax({
type: "GET",
url: "/update/data",
data: { 'id': id, 'email_address': email, 'username': user, 'password': pass },
success: function(data){
alert("updated")
}
});
});
</script>
The first alert correctly displays the new values, so that part is working.
The ruby script is as follows:
get '/update/data' do
DB[:users].where(:user_id => params[:id]).update(email_address:params[:email], username:params[:user], password:params[:pass])
redirect '/'
end
I think the DB query is written correctly but I think that the jquery and the ruby part of the script are not singing from the same hymn sheet.
When I checked my console this appeared:
"GET /update/data?user_id=31&email_address=miki%40nihon.jp.com&username=niyan123&password=braves098 HTTP/1.1"
which, in fact, reflects the changes I wanted to make. So something is happening, but the update does not then work, the record remains unchanged after the jquery completes its flow with the "updated" alert.
I have had a similar experience with my attempts to delete records inline.
What am I doing wrong? I am sure there are hideous errors all over the place. Should the ruby script be get, post or put for example?
Many thanks in advance for any help offered.