Skip to content

Instantly share code, notes, and snippets.

@dylanerichards
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save dylanerichards/5017132f85e4dd51eaef to your computer and use it in GitHub Desktop.

Select an option

Save dylanerichards/5017132f85e4dd51eaef to your computer and use it in GitHub Desktop.
Ajaxifying upvote/downvote process
<%= 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.
$(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