Thumbs_up Gem,ajax 的一个小问题
前几天我遇到了这个链接,我决定通过 ajax 实现这个投票系统: JQuery +thumbs_up gem 渲染投票数? .但问题是我几乎一步一步地遵循它,但似乎 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');
}
});
});
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');
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它对我来说工作正常..只需看看这个 http://jsfiddle.net/GLVQe/
确保你添加了 jQuery 库在此 jQuery 代码之前的文件。
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.