Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing data-method with javascript does not change what method the ajax calls users?

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.

like image 694
Jordan Scales Avatar asked Mar 30 '12 01:03

Jordan Scales


1 Answers

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
like image 135
Makibo Avatar answered Nov 10 '22 00:11

Makibo