Rails delete link JavaScript ajax call

安稳与你 提交于 2019-12-23 12:43:01

问题


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:


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
  }
});


来源:https://stackoverflow.com/questions/9234051/rails-delete-link-javascript-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!