I've run into a very strange issue that I'm having a tough time fixing. In my view, I have a link with data-remote="true"
and data-method="delete"
. When I click the link, I can see a DELETE
request to my rails server. The JS code returned then changes this link's properties, among which are the href
and data-method
.
Upon clicking this link again, my server is receiving a request to the new href
, but with the old data-method
, even though I have changed it from DELETE
to POST
(it still sends a DELETE
request).
If I refresh the page, however, the HTML is the same as the "new" HTML (changed with my returned JS), but it actually sends the right request type. This is why the issue is puzzling me.
Found a solution: Make sure you use the jQuery Element.data() Method for setting html data-attributes like "data-method" and so forth.
$(this).data('method', 'post'); # sets "data-method" attribute to "post"
$(this).data('method', 'delete'); # sets "data-method" attribute to "delete"
# "this" refers to the clicked link element for example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With