Skip to content

Instantly share code, notes, and snippets.

@guillaumecabanel
Last active March 13, 2017 18:23
Show Gist options
  • Save guillaumecabanel/044e9dfeab67157de5aa7b8e4118b18c to your computer and use it in GitHub Desktop.
Save guillaumecabanel/044e9dfeab67157de5aa7b8e4118b18c to your computer and use it in GitHub Desktop.
Use a confirmation delete for an object in rails
// JS
$('.delete-confirmable').click(function (e){
e.preventDefault();
$(this).hide();
$($(this).data('target')).removeClass('hidden');
})
# 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
<!-- In view -->
<%= confirmable_delete_for(@post) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment