I have a form_for(:remote=>true) working but I want to display a regular javascript confirmation box before submitting.
Is there anyway to achieve this?
Ideally some kind of :before or :onsubmit that would accept cancelling submission with something like: return confirm("Are you sure?"); or maybe a javascript event hook?
Thank you
You can do this easily with the
confirmoption:You need to be using a Rails UJS driver (enabled by default on new Rails projects) for this to work seamlessly.
There are also hooks into the form submission lifecycle you can access if you so wish:
ajax:beforeSend– triggered before the XHR request is made; return false to cancel the requestajax:success– triggered on a successful response from the serverajax:error– triggered on an error response from the serverajax:complete– triggered on either a success or error response from the serverSo, as an example, you should be able to mimic the
confirmform helper option with some code like this: