title: Rails destroy confirm with Jquery AJAX
body: <p>I have got this working for the most part. My rails link is:</p>
  
  <pre><code>&lt;%= link_to(image_tag('/images/bin.png', :alt =&gt; 'Remove'), @business, :class =&gt; 'delete', :confirm =&gt; 'Are you sure?', :id =&gt; 'trash') %&gt;
  </code></pre>
  
  <p><code>:class =&gt; "delete"</code> is calling an ajax function so that it is deleted and the page isn't refreshed that works great. But because the page doesn't refresh, it is still there. So my id trash is calling this jquery function:</p>
  
  <pre><code>$('[id^=trash]').click(function(){
              var row = $(this).closest("tr").get(0);
              $(row).hide();
              return false;
  });
  </code></pre>
  
  <p>Which is hiding the row of whatever trash icon i clicked on. This also works great. I thought I had it all worked out and then I hit this problem. When you click on my trash can I have this confirm box pop up to ask you if you are sure. Regardless of whether you choose cancel or accept, the jquery fires and it hides the row.  It isn't deleted, only hidden till you refresh the page.  I tried changing it so that the prompt is done through jquery, but then rails was deleteing the row regardless of what i choose in my prompt because the .destroy function was being called when the prompt was being called.</p>
  
  <p>My question really is how can i get the value to cancel or accept from the rails confirm pop up so that in my jquery I can have an if statement that hides if they click accept and does nothing if they click cancel.</p>
  
  <p>EDIT: Answering Question below.
  That did not work. I tried changing my link to:</p>
  
  <pre><code>&lt;%= link_to(image_tag('/images/bin.png', :alt =&gt; 'Remove'), @business, :class =&gt; "delete",  :onclick =&gt; "trash") %&gt;
  </code></pre>
  
  <p>and putting this in my jquery</p>
  
  <pre><code>function trash(){
  if(confirm("Are you sure?")){
      var row = $(this).closest("tr").get(0);
      $(row).hide();
      return false;
  } else {
  //they clicked no.
  }
  }
  </code></pre>
  
  <p>But the function was never called. It just deletes it with no prompt and doesn't hide it.
  But that gave me an idea.</p>
  
  <p>I took the delete function that ajax was calling</p>
  
  <pre><code>     $('a.delete').click (function(){
              $.post(this.href, {_method:'delete'}, null, "script");
              $(row).hide();
  });
  </code></pre>
  
  <p>And modified it implementing your code:</p>
  
  <p>remove <code>:confirm =&gt; 'Are you sure?'</code></p>
  
  <pre><code>$('a.delete').click (function(){
          if(confirm("Are you sure?")){
              var row = $(this).closest("tr").get(0);
              $.post(this.href, {_method:'delete'}, null, "script");
              $(row).hide();
              return false;
          } else {
              //they clicked no.
              return false;
          }
  
  });
  </code></pre>
  
  <p>Which does the trick.</p>