This gem adds some javascript to change the default behaviour of data-confirm processing for both Boostrap 2, 3 and 4.
The normal confirm dialog shows a text with buttons 'ok' and 'cancel'. More information is needed here for a user to make the right decision. This gem therefore also adds:
- data-confirm-fade (default: false)
- data-confirm-title (default: window.top.location.origin)
- data-confirm-cancel (default: 'cancel')
- data-confirm-cancel-class (default: 'btn cancel')
- data-confirm-proceed (default: 'ok')
- data-confirm-proceed-class (default: 'btn-primary')
This behaviour is similar to that of a "regular" confirm box in ways that it uses the same title and button labels. Defaults can be changed in two ways:
Changing all default values:
$.fn.twitter_bootstrap_confirmbox.defaults = {
fade: false,
title: null, // if title equals null window.top.location.origin is used
cancel: "Cancel",
cancel_class: "btn cancel",
proceed: "OK",
proceed_class: "btn proceed btn-primary",
modal_class: ""
};
Only changing one default value:
$.fn.twitter_bootstrap_confirmbox.defaults.proceed_class = "btn proceed btn-success";
Add this line to your application's Gemfile:
gem 'twitter-bootstrap-rails-confirm'
And then execute:
$ bundle
Note: jQuery-Rails should be > 2.2.1. The exact required version has not yet been determined.
Add it to your application.js, anywhere after you require jquery_ujs:
//= require twitter/bootstrap/rails/confirm
Next... nothing. There is nothing you need to do to get this working. A helper could be useful for handling large amount of destroy buttons:
def destroy_link_to(path, options)
link_to t('.destroy'), path,
:method => :delete,
:class => 'btn',
:confirm => t('.destroy_confirm.body', :item => options[:item]),
'data-confirm-fade' => true,
'data-confirm-title' => t('.destroy_confirm.title', :item => options[:item]),
'data-confirm-cancel' => t('.destroy_confirm.cancel', :item => options[:item]),
'data-confirm-cancel-class' => 'btn-default'),
'data-confirm-proceed' => t('.destroy_confirm.proceed', :item => options[:item]),
'data-confirm-proceed-class' => 'btn-danger'
end
See Twitter Bootstrap Rails Confirm Test for a simple app that loads Bootstrap and this gem.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- (rvanlieshout) Added bootstrap 3 support
- (rvanlieshout) Problems with using event constructors
- (stevelacey) Avoid appending custom proceed class to btn-primary, refactored dialog JS
- (zsy056) When message is numerical, the call replace on message will cause a TypeError
- (kramerc) Prevent the default action on the modal's buttons.
- (taavo) click handler returns false
- (stevelacey) Swap out new lines for br's in message
- (DavyCardinaal) Confirmbox defaults: Cancel button btn-default (Bootstrap 3 only)
- (digitalfrost) Create "click" event using an event constructor
- First 'official' release
- Many thanks to taavo for his contributions