Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRaouf Tiliouine
15,192 PointsSweetAlert with Rails
Hi,
Just wondering how I could go about using SweetAlert with a destroy button in a Rails scaffold.
Thanks in advance!
2 Answers
Maciej Czuchnowski
36,441 PointsAdd this to your Gemfile:
gem 'sweet-alert'
gem 'sweet-alert-confirm'
Run bundle install
.
Add this to your app/assets/javascripts/application.js:
//= require sweet-alert
//= require sweet-alert-confirm
and add this to your app./assets/stylesheets/application.css:
*= require sweet-alert
I just tried it, works like a charm. Some custom options are given here: https://github.com/mois3x/sweet-alert-rails-confirm
Raouf Tiliouine
15,192 PointsHey Maciej,
Thanks for the answer. I actually got it to work another way. I added this coffee script to one of my scaffolds' javascript file (its was a student scaffold, so the path is /app/assets/javascript/students.coffee)
$.rails.allowAction = (link) ->
return true unless link.attr('data-confirm')
$.rails.showConfirmDialog(link) # look below for implementations
false # always stops the action since code runs asynchronously
$.rails.confirmed = (link) ->
link.removeAttr('data-confirm')
link.trigger('click.rails')
$.rails.showConfirmDialog = (link) ->
swal {
title: 'Are you sure?'
text: 'This cannot be undone!'
type: 'warning'
showCancelButton: true
confirmButtonColor: '#DD6B55'
confirmButtonText: 'Delete'
cancelButtonText: 'Cancel'
closeOnConfirm: false
closeOnCancel: false
}, (isConfirm) ->
if isConfirm
swal 'Deleted!', 'Student has been deleted!.', 'success', $.rails.confirmed link
else
swal 'Cancelled', 'Student delete has been cancelled', 'error'
return
And that also worked!
Sources:
http://www.pjmccormick.com/nicer-rails-confirm-dialogs-and-not-just-delete-methods http://lesseverything.com/blog/archives/2012/07/18/customizing-confirmation-dialog-in-rails/
I did find that gem you mentioned, but I just didn't bother looking too much into it, as I tried to understand how I could change the default behaviour of the button. This answer is just for 'education purposes' for anyone looking at this thread, and wants to know more about this.