前几天我发现了这个链接,我决定通过ajax实现这个投票系统:JQuery + thumbs_up gem render vote count? 。但问题是我几乎一步一步地遵循它,但似乎 ajax 不起作用。有人可以帮忙吗?谢谢!
微帖子 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');
}
});
});
最佳答案
它对我来说工作得很好..看看这个 http://jsfiddle.net/GLVQe/
确保在此 jQuery 代码之前添加了 jQuery 库文件。
关于javascript - Thumbs_up Gem,ajax 的一个小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401531/