【发布时间】:2012-03-13 04:16:56
【问题描述】:
前几天我遇到了这个链接,我决定通过 ajax 来实现这个投票系统:JQuery + thumbs_up gem render vote count?。但问题是我几乎一步一步地遵循它,但似乎 ajax 不起作用。任何人都可以帮忙吗?谢谢!
微博控制器
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');
}
});
});
【问题讨论】:
-
你的点击处理程序被调用了吗?
-
@muistooshort 我不知道该怎么做,对 jquery 还是很陌生,抱歉,谢谢你的帮助!
-
您可以在点击处理程序中添加一个
console.log(...)调用,然后观察 JavaScript 控制台中显示的内容。 -
@muistooshort 听起来不是很需要,但你能指出在哪个区域添加它吗?很抱歉这些问题,已经为此工作了几个小时,但什么都做不好
-
你可以把它放在
$(".CounterButton").click(function(){之后,只是一个简单的console.log('click handler called'),看看它是否在你期望的时候被调用。
标签: javascript jquery ruby-on-rails ajax ruby-on-rails-3