百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

data-confirm-modal

> 编程语言
开源

使 Rails 的 link_to confirm: 'foo' 创建一个 Bootstrap 模态框,而不是调用浏览器的 confirm() API。

268 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使 Rails 的 link_to confirm: 'foo' 创建一个 Bootstrap 模态框,而不是调用浏览器的 confirm() API。

Data-Confirm Modal

Uses Bootstrap's modals in place of the browser's builtin confirm() API for links generated through Rails' helpers with the :confirm option.

Any link with the data-confirm attribute will trigger a Bootstrap modal.

HTML in the modal supported, and also the ability to have the user input a certain value, for extra willingness confirmation (inspired by GitHub's "delete repository" function).

Installation

Sprockets

Add this line to your application's Gemfile:

gem 'data-confirm-modal'

The library supports Bootstrap 3 and 4. If you are stuck on Bootstrap 2.3, you must use the bootstrap2 branch:

gem 'data-confirm-modal', github: 'ifad/data-confirm-modal', branch: 'bootstrap2'

Then execute:

$ bundle

And then require the Javascript from your application.js:

//= require data-confirm-modal

Webpacker

Gem is not required for this use case.

Add the following package:

bash
yarn add data-confirm-modal

Autoload jQuery in config/webpack/environment.js:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')

environment.plugins.prepend(
  'Provide',
  new webpack.ProvidePlugin({
    jQuery: 'jquery',
  })
)

module.exports = environment

And then require the JavaScript from your pack's application.js, after require('@rails/ujs').start():

require('data-confirm-modal')

Usage

With Rails: example B3, example B4

By default, the Gem's Javascript overrides Rails' [data-confirm behaviour][] for you, with no change required to your code. The modal is applicable to , and `` elements by default.

The modal's title text can be customized using the data-title attribute. If data-title is not defined it falls back to the title attribute.

The modal's 'confirm' button text can be customized using the data-commit attribute.

Add a data-verify attribute to your input if you want an extra confirmation from the user. The modal will contain an extra text input, and the user will be asked to type the verification value before being allowed to proceed.

You can set global setting using dataConfirmModal.setDefaults, for example:

dataConfirmModal.setDefaults({
  title: 'Confirm your action',
  commit: 'Continue',
  cancel: 'Cancel'
});

To restore default settings use dataConfirmModal.restoreDefaults().

Without Rails, with data attributes: example B3, example B4

Given an element with data-confirm attributes in place, such as

you can then invoke .confirmModal() on it using:

$('#foo').confirmModal();

that'll display the confirmation modal. If the user confirms, then the #foo link will receive a click event.

Without Rails, without data attributes: example B3, example B4

Use dataConfirmModal.confirm() passing any of the supported options, and pass an onConfirm and onCancel callbacks that'll be invoked when the user clicks the confirm or the cancel buttons. onHide callback will be invoked when the modal closes.

dataConfirmModal.confirm({
  title: 'Are you sure?',
  text: 'Really do this?',
  commit: 'Yes do it',
  cancel: 'Not really',
  zIindex: 10099,
  onConfirm: function() { alert('confirmed') },
  onCancel:  function() { alert('cancelled') },
  onHide:    function() { alert('hidden') }
});

Modal Options

The default bootstrap modal options can be passed either via JavaScript or through data attributes.

 $('#foo').confirmModal({backdrop: 'static', keyboard: false});

or

Authors

  • Marcello Barnaba (@vjt)
  • LLeir Borras Metje (@lleirborras)
  • The Open Source World

Background

Spinned off a corporate IFAD application in which an user did too much damage because the confirm wasn't THAT explicit ... ;-).

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 12 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言