Thumbs_up Gem,ajax的一个小问题

时间:2012-02-22 19:23:12

标签: javascript jquery ruby-on-rails ajax ruby-on-rails-3

前几天我遇到了这个链接,我决定通过ajax实现这个投票系统:JQuery + thumbs_up gem render vote count?。但问题是我几乎一步一步地遵循它,但似乎ajax不起作用。有人可以帮忙吗?谢谢!

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');
        }
    });​
});

1 个答案:

答案 0 :(得分:3)

这对我来说很好..请看看这个http://jsfiddle.net/GLVQe/

确保在此jQuery代码之前添加了jQuery库文件。