I have a partial _table_row
this partial holds my html for the output of @admin_products.all
In this partial I have a soft delete button which initiates a twitter bootstrap popover, this popover has the hard delete button for the record from the database.
To get the button into the popover currently I’ve included the HTML content in the partial _table_row
this as you can imagine repeats the HTML by the number of records in my database.
What I’m trying to achieve is to keep the HTML for the popover in index.html.erb
and to dynamically add the destroy button into the popover with the correct ID. This is where I’m having difficulty.
I’ve tried to use
$('.deleteButton').popover({
html: true,
content: $('#popoverDestroy').html() + <%= link_to 'Delete', admin_product, method: :delete, class: 'btn btn-danger destroyButton', remote: true %>,
placement: 'left'
});
But as you can understand it will fail as it doesn’t know what admin_product is, how would you associate it correctly?
3
Answers
Add the HTML that you want as a data attribute on the soft delete button, then pull it out again in javascript:
In the partial:
Then in JavaScript:
You can pass the product id as an html attribute. In you
_table_row.html.erb
:<a class='deleteButton' data-productId='<%= admin_product.id %>'>Soft Delete</a>
And then in your javascript, you can access the clicked button with the
this
reference if you pass a function tocontent
(reference):Instead of breaking best practice and placing your javascript inline you could actually have the “soft” link actually have the correct URL.
You could do this by having your “soft” link actually point to the delete url and having an event listener which cancels out the click and opens the popover.
This is more inline with the idea of progressive enhancement since you take a normal HTML link element and add behavior.