Reputation: 471
I came across this link the other day and I decided to implement this voting system through ajax: JQuery + thumbs_up gem render vote count? . But the problem is I pretty much followed it step by step but it seems as though the ajax isn't working. Can anyone help? Thank you!
Micropost Controller
class MicropostsController < ApplicationController
def vote_up
@micropost = Micropost.find(params[:id])
current_user.vote_exclusively_for(@micropost)
end
def vote_down
@micropost = Micropost.find(params[:id])
current_user.vote_exclusively_against(@micropost)
end
end
votecount.html.erb
<div class='Counter'>
<span class='CounterNum'><%= @micropost.votes_for %></span>
<a href="#" class='CounterButton b2' updown="up" theid="123">
<span class='CounterIcon'></span>
</a>
<a href="#" class='CounterButton b2' updown="down" theid="123">
<span class='CounterIcon'></span>
</a>
</div>
votecount.js
$(document).ready(function(){
$(".CounterButton").click(function() {
var val = $(this).attr('updown');
var theid = $(this).attr('theid');
if (val == "up") {
console.log('up');
} else {
console.log('down');
}
});
});
Upvotes: 2
Views: 244
Reputation: 3233
It is working fine for me .. Just have look at this http://jsfiddle.net/GLVQe/
Make sure you added jQuery library file before this jQuery code.
Upvotes: 3