public
Last active

A better Rails confirmation dialog

  • Download Gist
jquery_ui_confirm.js.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# Replace Rails' confirmation dialog with a translatable jQuery UI modal dialog.
# Requires i18n-js (https://github.com/fnando/i18n-js)
# Translation strings used: confirm.title, confirm.ok, confirm.cancel
$ ->
$.rails.confirm = (message) ->
$("#confirm_dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + message + '</p>')
false
if $('[data-confirm]')
$('body').append("<div id='confirm_dialog' title='" + I18n.t('confirm.title') + "'></div>")
diag_elem = $('#confirm_dialog')
diag_elem.dialog
autoOpen: false
modal: true
open: ->
$('button:first').blur() # prevent jQuery from focusing it
$('[data-confirm]').each ->
link_elem = $(this)
method = link_elem.attr('data-method')
method = 'get' unless method?
link_elem.click (e) ->
e.preventDefault()
diag_btns = {}
diag_btns[I18n.t('confirm.ok')] = ->
$.ajax
type: 'post'
async: false
url: link_elem.attr('href')
data: { _method: method }
dataType: 'html'
success: (x) -> $('body').html(x)
diag_elem.dialog('close')
diag_btns[I18n.t('confirm.cancel')] = ->
diag_elem.dialog('close')
diag_elem.dialog(buttons: diag_btns)
diag_elem.dialog('open')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.