Skip to content

Instantly share code, notes, and snippets.

@ntamvl
Last active April 10, 2024 14:17
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 ntamvl/5780f851213d7c980d4ab926e1092a64 to your computer and use it in GitHub Desktop.
Save ntamvl/5780f851213d7c980d4ab926e1092a64 to your computer and use it in GitHub Desktop.
Use Sweetalert2 on Rails 7

Use Sweetalert2 on Rails 7

Install sweetalert2 package

yarn add sweetalert2

OR if you use importmap

./bin/importmap pin sweetalert2

Edit app/javascript/application.js

// app/javascript/application.js
import Swal from 'sweetalert2'

Turbo.setConfirmMethod((message, element) => {
  return new Promise((resolve, reject) => {
    Swal.fire({
      title: "Notice!",
      text: message || "Are you sure?",
      icon: 'warning',
      showCancelButton: true
    }).then((result) => {
      resolve(result.isConfirmed)
    }).catch(error => { reject(error) })
  })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment