Rails 删除来自 javascript 的 link 个电话

Rails delete link call from javascript

有了 rails 我可以做到:

<%= link_to("Delete", product, 
            :method => :delete, 
            data: {confirm: 'Are you sure?.'}
            id: 'discard_btn') %>

而且我知道,当收到点击时,标准的 javascript 确认对话框将询问 'Are you sure?' 问题,如果点击确定,则发送请求。

我想用具有相同行为的漂亮的 Bootbox 确认对话框更改此普通对话框。我尝试这样做:

$('#employee_discard').click(function(e){
  bootbox.confirm("Are you sure?", function(result) {
    // How to send the delete request from this callback?
  });
  // Prevent the request to be sent.  This works/  
  return false;
});

但我的问题是 Bootbox 使用回调来处理它的事件,只在回调中写一个 return true 是行不通的。

如何从 javascript 发送删除请求?

像这样的东西应该可以工作:

$.ajax({
    type: "DELETE",
    url: "/products/1"
})

并且在控制器中你需要添加一个js格式的响应

respond_to do |format|
  format.js { render "something.js.erb" }
end

最后,something.js.erb将被执行。比如可以刷新当前页面

location.reload();