Rails 4: How to get a sweet confirmation signal?

I start with rails and I want to use Sweet Alert to replace the ugly basic confirmation messages when deleting records. I added a sweet alert and sweet-alert-confirm to my gem file. I did exactly what they said in their Read Me, but it doesn’t work, I can delete entries, but it immediately left without any confirmation of any confirmation.

Gemfile

... gem 'sweet-alert' gem 'sweet-alert-confirm' ... 

Application.jssupported directives.

 //= require jquery //= require jquery_ujs //= require turbolinks //= require jquery.turbolinks //= require bootstrap-sprockets //= require sweet-alert //= require sweet-alert-confirm //= require_tree . 

stylesheet.css

 /* ... *= require_self *= require sweet-alert *= require_tree . */ 

index.html.erb

 ... <%= link_to 'Destroy', ice_cream, method: :delete, data: { confirm: 'Are you sure?' } %> ... 

Also, there are some things that I don’t know whether it is worth mentioning when I open firebug, I find the following error.

Error Error Error

Hope you help me, thanks.

+6
source share
5 answers

Thanks for the unity for the great help !, But again, I found the answer myself ...

The problem seems to be related to sweetalert gem, So

  • I deleted it with

     gem uninstall sweetalert 
  • I installed sweetalert2 using Rails Assets Web Sit e by adding the following line to my Gemfile

     gem 'rails-assets-sweetalert2', source: 'https://rails-assets.org' 
  • Start the beam

  • Reorder application.js this way

     //= require sweetalert2 //= require jquery //= require jquery_ujs //= require turbolinks //= require jquery.turbolinks //= require bootstrap-sprockets //= require sweet-alert-confirm //= require_tree . 
  • Reorder application.css this way

      *= require_self *= require sweetalert2 *= require_tree . */ 

I saved the sweet-alert-confirm gem and it worked perfectly. Now I find this sweet message every time I try to delete a record without adding any lines of code ...

enter image description here

Note: please explain why you vote on the issue before you do.

+7
source

I'm not too sure why your question went, but I actually ran into the same problem a few days ago.

Here is a solution without using gems.

  • Uploading files for Sweet Alerts 2. Adding files to upload using assets. This will allow you to create alerts using the sweetAlert or swal function.

As already noted, you will need to write your own event handler for confirmation alerts and will require quite a bit of work, as this is a POST and DELETE request.

  1. For such requests, you can use this Javascript code to overwrite the allowAction method. Answer adapted from: http://thelazylog.com/custom-dialog-for-data-confirm-in-rails/

In the application.js file or any js file:

 //Override the default confirm dialog by rails $.rails.allowAction = function(link){ if (link.data("confirm") == undefined){ return true; } $.rails.showConfirmationDialog(link); return false; } //User click confirm button $.rails.confirmed = function(link){ link.data("confirm", null); link.trigger("click.rails"); } //Display the confirmation dialog $.rails.showConfirmationDialog = function(link){ var message = link.data("confirm"); swal({ title: message, type: 'warning', confirmButtonText: 'Sure', confirmButtonColor: '#2acbb3', showCancelButton: true }).then(function(e){ $.rails.confirmed(link); }); }; 

Tried with gems, but it didn’t work for me.

+1
source

I use TurboLinks 5, and none of the solutions provided worked for me, except this one. Probably not the cleanest solution, but it works ...

Using the sweetalert2 gem, add the following to the application.js file.

 //Override the default confirm dialog by rails $.rails.allowAction = function(link){ if (link.data("confirm") == undefined){ return true; } $.rails.showConfirmationDialog(link); return false; } //User click confirm button $.rails.confirmed = function(link){ link.data("confirm", null); Turbolinks.visit(link.attr('href')) } //Display the confirmation dialog $.rails.showConfirmationDialog = function(link){ var message = link.data("confirm"); swal({ title: message, type: 'warning', confirmButtonText: 'Continue', confirmButtonColor: '#0A6EFF', showCancelButton: true }).then(function(e){ $.rails.confirmed(link); }); }; 
+1
source
  • Add this to your gemfile:

    gem 'sweetalert-rails'

  • Add this to your assets /javascripts/application.js

    // = require sweet-alert

  • Add this to your assets / stylesheets /application.css

    * = require sweet-alert

  • In erb file add this line

    "sweet (# {params [: id]}, 'Are you sure you want to delete ?,' # {posts_path (params [: id])} ',' Delete all ')",: class => "delete -all red-text "%>

In js file add this code

This code can be used for all alerts through the project, passing the appropriate parameters.

 function sweet(id, text, href, button_text) { timer: 4000 swal({ title: "Warning!", text: text, showCancelButton: true, confirmButtonColor: "#FF6633", confirmButtonText: button_text, cancelButtonText: "Cancel", closeOnConfirm: true, closeOnCancel: true }, function(isConfirm) { if (isConfirm) { location.href = href; } }); } 
0
source

I was not able to get any of the above solutions to work. (Rails 5, Turbolinks 5).

Finally, it worked for me to add the following coffeescript file:

 ready = -> $('[data-sweet-alert-confirm]').on 'click', sweetAlertConfirm $(document).ready(ready) $(document).on('page:load', ready) 
0
source

All Articles