Last active
August 29, 2015 14:06
-
-
Save dylanerichards/5017132f85e4dd51eaef to your computer and use it in GitHub Desktop.
Ajaxifying upvote/downvote process
This file contains hidden or 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
| <%= guide.get_upvotes.count %><%= link_to like_guide_path(guide), method: :put, class: 'upvote', remote: true do %> | |
| <%= link_to dislike_guide_path(guide), method: :put, class: 'downvote', remote: true do %> | |
| ## Giving the link a class of upvote/downvote and setting remote to true. |
This file contains hidden or 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
| $(document).ready(function() { | |
| $('.upvote').click(function() { | |
| // find the text value of the clicked element, turn it into an integer, and increment it by 1 | |
| queue = parseInt($(this).parents('tr').children().eq(2).children().text()) + 1; | |
| // actually set that value to what we found above | |
| $(this).parents('tr').children().eq(2).children().text(queue); | |
| $(this).hide(); | |
| // send a put request to the upvote route | |
| $.put('/guides/upvote'); | |
| }); | |
| $('.downvote').click(function(){ | |
| queue = parseInt($(this).parents('tr').children().eq(2).children().text()) - 1; | |
| $(this).hide(); | |
| $(this).parents('tr').children().eq(2).children().text(queue); | |
| $.put('guides/downvote'); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment