Last active
March 13, 2017 18:23
-
-
Save guillaumecabanel/044e9dfeab67157de5aa7b8e4118b18c to your computer and use it in GitHub Desktop.
Use a confirmation delete for an object in rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JS | |
$('.delete-confirmable').click(function (e){ | |
e.preventDefault(); | |
$(this).hide(); | |
$($(this).data('target')).removeClass('hidden'); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper | |
def confirmable_delete_for(target) | |
target_name = target.class.to_s.downcase | |
target_path = "#{target_name.pluralize}/#{target.id}" | |
"<a href=\"#\" class=\"text-danger delete-confirmable\" | |
data-target=\"#delete-#{target_name}-#{target.id}\"> | |
Delete</a> | |
<a class=\"text-danger hidden\" id=\"delete-#{target_name}-#{target.id}\" | |
data-disable-with=\"<i class='fa fa-spinner fa-spin'></i> deleting...\" | |
rel=\"nofollow\" data-method=\"delete\" | |
href=\"#{target_path}\">Confirm?</a>".html_safe | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- In view --> | |
<%= confirmable_delete_for(@post) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment