title: Sinatra (Ruby) - Posting with Ajax
body: <p>I'm attempting to submit some data on the fly to a route using $.post and I am currently faced with a mysterious error that I cannot get my head around.</p>
  
  <p>Here's my route...</p>
  
  <pre><code>post "/share" do
      sid   = params[:sid]
      user  = (session[:level] == 1) ? params[:user] : session[:user]
      # Database
      require_relative "../../database/db.rb"
      db = DBShare.new
      db.share(sid, user)
  end
  </code></pre>
  
  <p>Here's what I get when my $.post request is sent to the route...</p>
  
  <pre><code>!! Unexpected error while processing request: undefined method `close' for 1:Fixnum
  </code></pre>
  
  <p>The ajax call returns in the .fail(), but the database call is completed (I use Sequel for the database call which executes a stored procedure) - interestingly it calls the procedure twice on the first call, and only once on subsequent calls.</p>
  
  <p>I thought it might be because my route was not returning a content header - so I added "success" after the db call.</p>
  
  <p>This resulted in the jquery returning in the .success() block, but the database call was not completed.</p>
  
  <p>Here's my jQuery call</p>
  
  <pre><code>// Share or unshare
  $(document).on('click', '.share-unshare', function(event) {
      if (event.type == 'click') {
          var element     = $(this);
          var text        = $(this).text();
          var sid         = $(this).attr('sid');
          var r = '';
  
          // Unshared
          if (text == "Unshare") {
              r = confirm("Are you sure you want to unshare this?");
              if (r == false) {
                  return false;
              }
              share(sid, element);
  
          // Shared
          } else {
              r = confirm("Are you sure?");
              if (r != false) {
                  share(sid, element);
              }
          }
  
      }
  
      function share(sid, element) {
          var user = $('#details').attr('user');
          $.get('/share/', { 'sid' : sid, 'user': user })
              .success(function(data) {
                 if (text == "Share") {
                     element.html("Unshare");
                 } else {
                     element.html("Share");
                 }
              })
              .fail(function() {
                 alert("Something went wrong - unable to share / unshare");
              })
              .always();
      }
  
  });
  </code></pre>
  
  <p>Here's the DB call (the procedure works fine through so not concerned with the PL/SQL)</p>
  
  <pre><code>    def share(sid, user)
          sql = "
          BEGIN
              pa_share.pr_share_handle(?, ?);
          END;
          "
          ds = $db_app[sql, sid, user]
      end
  </code></pre>
  
  <p>At this stage - I am thoroughly baffled! Does anyone have any ideas what it might be please?</p>
  
  <p>Many thanks</p>
  
  <p>Ash</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I managed to fix this issue by adding <strong>ds.insert</strong> after my params were bound in the db call..</p>
  
  <pre><code>def share(sid, user)
      sql = "
      BEGIN
          pa_share.pr_share_handle(?, ?);
      END;
      "
      ds = $db_app[sql, sid, user]
      **ds.insert**
  end
  </code></pre>