:delete, data: {confirm: 'Are yo...">

Rails remove link call from javascript

With rails, I can do this:

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

      

and I know that when the click is received, the standard JavaScript confirmation dialog will ask "Are you sure?" the question of sending a request if OK is clicked.

I want to modify this simple dialog with a nice Bootbox dialog with the same behavior. I tried to do this:

$('#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;
});

      

But my problem is that Bootbox uses callbacks to handle its events and just writing the returned true inside the callback won't work.

How to send a delete request from javascript?

+3


source to share


1 answer


Something like this should work:

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

      

And in the controller you need to add the js formatted response



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

      

Finally, something.js.erb file will be executed. For example, you can refresh the current page

location.reload();

      

+2


source







All Articles