Thumbs_up Gem,ajax 的一个小问题

发布于 2025-01-08 03:26:57 字数 1361 浏览 0 评论 0原文

前几天我遇到了这个链接,我决定通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-01-15 03:26:57

它对我来说工作正常..只需看看这个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文