3

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, a standard javascript confirm dialog will ask the 'Are you sure?' question sending the request if an OK is clicked.

I want to change this plain dialog with a beautiful Bootbox confirm 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 a return true inside the callback will not work.

How can I send a delete request from javascript?

2
  • The click handler had already returned false. In the callback you test the result value then either $.post(delete_url, data, callback_func) or submit a form $(form).submit(). This answer explains the same problem. Commented Jul 6, 2015 at 17:35
  • Thanks @Jasen Your answer reference solved my problem. Commented Jul 7, 2015 at 12:41

1 Answer 1

2

Something like this should work:

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

And in the controller you need to add a response in js format

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

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

location.reload();
Sign up to request clarification or add additional context in comments.

1 Comment

Gracias @Diego. I finally solved my problem with jasen reference, but your answer makes sense, and I'll accept 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.