4

I want to create an ajax delete call. When the link is clicked, the confirm box should appear and then the p tag fades out (comment). The problem is just how the ajax call should be and how to show the confirm box.

HTML view:

<a rel="nofollow" data-method="delete" data-confirm="Er du sikker?" class="softdelete" href="/blogs/5/comments/18">slet</a>

jQuery:

<script type="text/javascript">
$(document).ready(function() {
    $('.softdelete').click(function () {
        var Url = $(this).attr('href');
        var Data = $(this).attr('data-method');
        $(this).closest('p').fadeOut(1000);
        $.post(Url);
        return false;
    });
});
</script>

When clicked on the delete link, the comment fades out, but it is not destroyed. Also no confirm box appeared.

1 Answer 1

7

You're using a post request, so the action isn't properly routed: Rails expects a delete request.

Here is the way to proceed with jQuery:

$.ajax({
  url: your_url,
  type: 'DELETE',
  success: function(result) {
    // Do something with the result
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

How to show the Confirm box? And is there not a shortcut for a delete request like post?
there are countless ways to create a dialog box, it really depends on what you expect. I don't know any shortcut for the delete method.
what about the built in javascript dialag box in Rails? Is it not possible to call it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.