问题
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