Search code examples
ruby-on-railsujs

Catch rails confirm box action


I have the following rails button:

<%=
  link_to "Remove Product", product_path(product),
    remote: true,
    method: :delete,
    data: { confirm: 'Are you sure you want to delete this product?' }
%>

I'm using rails-ujs to show the confirm() dialog; is there an easy way catch the confirm answer with javascript?


Solution

  • There is a confirm:complete callback; this is not documented, as far as I can see, I found it by looking at the source.

    This gets executed right after the confirm() call:

    $(document).on 'confirm:complete', (e, answer) ->
      alert "Your answer was: #{answer}"
      return true # You can still cancel by returning false here