0

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.

Slartibartfast
  • 8,735
  • 6
  • 41
  • 45
user1903663
  • 1,713
  • 2
  • 22
  • 44
  • Have you tried running the [Sequel console](http://sequel.rubyforge.org/rdoc/files/doc/bin_sequel_rdoc.html) and checking that the query you're using works? Do you have a model written for the `users` table? – ian May 12 '13 at 17:45
  • thanks, yes I have checked it. The following works: DB[:users].where(:user_id => 38).update(:email_address => 'miki@nihon.com', :username => 'niyan94', :password => 'mypas') but exact same, relying on passed paramaters, does not: DB[:users].where(:user_id => params[:id]).update(:email_address => params[:email], :username => params[:user], :password => params[:pass]) N.B. No mismatch (usr_id => id). – user1903663 May 13 '13 at 02:13
  • First line of the route, put `warn params.inspect` to see what's in there, or you could use [Pry-Debugger](https://rubygems.org/gems/pry-debugger). Check out this article on [which HTTP verb](http://amundsen.com/blog/archives/1122) might be best for you. I'm thinking `PATCH`. – ian May 13 '13 at 12:49
  • Thanks, nice idea. This is what I get with params.inspect: {"user_id"=>"56", "email_address"=>"rich", "username"=>"rich", "password"=>"rich"} where do I go form here? How do I make sure the params are collected and used to update hte DB, which is still not happening? – user1903663 May 14 '13 at 06:50
  • This works: DB[:users].where(:user_id => 56).update(:email_address => 'richard@pachme.com', :username => 'miki', :password => 'text') but this does not: DB[:users].where(:user_id => params[:user_id]).update(:email_address => params[:email], :username => params[:user], :password => params[:pass]) The terminal shows this: {"user_id"=>"57", "email_address"=>"rich", "username"=>"rich", "password"=>"rich"} 127.0.0.1 - - [14/May/2013 16:01:32] "GET /update/data?user_id=57&email_address=rich&username=rich&password=rich HTTP/1.1" 1 - 0.2393 Mighty strange! – user1903663 May 14 '13 at 07:04
  • Fixed! Woopee. It seems there was a mismatch (as suggested by Slartibartfast initially) in the param names. Not the exact one that he noticed but user and pass. So correct query is: DB[:users].where(:user_id => params[:user_id]).update(:email_address => params[:email_address], :username => params[:username], :password => params[:password]) Tanks for all the comments, guys. I hope this will help other people! – user1903663 May 14 '13 at 07:13
  • Glad you got it to work :) It's just a few lines of code to set up a model for that, and then the call would be `user = User[params.delete(:user_id).to_i]; user.update params`. In the long run it's much more convenient. – ian May 14 '13 at 12:11

1 Answers1

0

If this is indeed verbatim of your code, than you have a naming mismatch somewhere: your param is called user_id, but you are querying for params[:id]

Are you sure you are looking at the right peace of Javacript code, or that your browser didn't cache old code or something?

Slartibartfast
  • 8,735
  • 6
  • 41
  • 45