Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created October 29, 2012 22:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyhunak/3977007 to your computer and use it in GitHub Desktop.
Save seyhunak/3977007 to your computer and use it in GitHub Desktop.
Custom confirmation dialog in Rails
# Coffeescript
$.rails.allowAction = (link) ->
return true unless link.attr('data-confirm')
$.rails.showConfirmDialog(link) # look bellow for implementations
false # always stops the action since code runs asynchronously
$.rails.confirmed = (link) ->
link.removeAttr('data-confirm')
link.trigger('click.rails')
### Boostrap
$.rails.showConfirmDialog = (link) ->
message = link.attr 'data-confirm'
html = """
<div class="modal" id="confirmationDialog">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Are you sure Mr. President?</h3>
</div>
<div class="modal-body">
<p>#{message}</p>
</div>
<div class="modal-footer">
<a data-dismiss="modal" class="btn">Cancel</a>
<a data-dismiss="modal" class="btn btn-primary confirm">OK</a>
</div>
</div>
"""
$(html).modal()
$('#confirmationDialog .confirm').on 'click', -> $.rails.confirmed(link)
### jQueryUI
$.rails.showConfirmDialog = (link) ->
message = link.attr 'data-confirm'
html = """
<div id="dialog-confirm" title="Are you sure Mr. President?">
<p>#{message}</p>
</div>
"""
$(html).dialog
resizable: false
modal: true
buttons:
OK: ->
$.rails.confirmed link
$(this).dialog "close"
Cancel: ->
$(this).dialog "close"
### Noty (http://needim.github.com/noty/)
$.rails.showConfirmDialog = (link) ->
message = link.attr 'data-confirm'
okButton =
type: 'btn btn-primary'
text: 'Ok'
click: (noty) -> $.rails.confirmed(link); noty.close()
cancelButton =
type: 'btn btn-danger'
text: 'Cancel'
click: (noty) -> noty.close()
noty
text: message
buttons: [okButton, cancelButton]
closable: false
timeout: false
modal: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment