javascript - JQuery +thumbs_up gem 渲染投票数?

标签 javascript jquery ruby-on-rails ruby-on-rails-3 voting

插件: Thumbs Up & JQuery 1.5.2(另一个旧的 gem 需要)

当用户对帖子投票时,我尝试在不使用完整 HTTP 请求的情况下呈现更新的投票计数。目前,每次投票都会刷新页面。

帖子 Controller

def vote_up
  post = Post.find(params[:id])
  current_user.vote_exclusively_for(post)
  respond_to do |format|
    format.js
    format.html {rRedirect_to :back}
  end
end

def vote_down
  post = Post.find(params[:id])
  current_user.vote_exclusively_against(post)
  respond_to do |format|
    format.js
    format.html {redirect_to :back}
  end
end

投票 View (每个帖子 div 左侧都有一个投票 div(digg/reddit 样式),右侧有内容)

 <div class="post">
 <div class="vote">
 <div class="votewrapper">
  <span class="votecount">
  <%= post.votes_for - post.votes_against %>
    </span>
  <div class="votebtn">
   <%= link_to image_tag('vote.png'), vote_up_post_path(post), :method => :post, :format => :js %>
     </div>
   </div>
   </div>
   <div class="postcontent">
   all the post content, timestamp stuff, etc...
   </div>
   </div>

vote_up.erb.js(在帖子文件夹中)。

$(".votecount").html(
"<%= escape_javascript post.votes_for - post.votes_against %>");

我已经被困在这个问题上有一段时间了,非常感谢您能提供的任何帮助。我已经看过 Jquery Railscast 并浏览了其他 Stackoverflow 答案,但我对 Jquery 仍然很菜鸟。

最佳答案

您似乎希望将 View 代码分离为部分代码,并且在提供评级时仅刷新一个部分代码。

对于您的 Controller ,而不是:

respond_to do |format|
    format.js
    format.html {redirect_to :back}
  end

做类似的事情:

render :partial => "voutecount"

在您看来,将 votewrapper div 移出到同一目录中名为“_votecount.html.erb”的新文件中,并改为使用渲染代码:

 <%= render :partial => "votecount" %>

如果你想在刷新时阻止评级(推荐),那么你可能需要ajaxify调用并在js中更多地控制它。因此,将您的 JavaScript 包含在 View 中:

<%= javascript_include_tag 'votecount' %>

将您的 link_to 替换为良好的 ol' html 以获取更多信息:

<a href="" class="ratelink" updown="up" theid="123"><img src = "...."></a>
<a href="" class="ratelink" updown="down" theid="123"><img src = "...."></a>

并在 public/javascripts 文件夹中创建一个新的 votecount.js,其中包含以下内容:

  $(function(){
        $(".ratelink").click(function(){
            var val = $(this).attr('updown');
            var theid = $(this).attr('theid');
            $("#votewrapper").block({ //blocks rate-rates while processing
                message: null,
                overlayCSS: {
                    backgroundColor: '#FFF',
                    opacity: 0.6,
                    cursor: 'default'
                },
            });
        if (val == "up") {
        $.ajax({
                type: 'PUT',
                url: "/mymodel/voteup?id="+theid,
                success: function(){
                            $("#votewrapper").unblock();
                            }   
                   });
        } else {
             $.ajax({
                type: 'PUT',
                url: "/mymodel/votedown?id="+theid,
                success: function(){
                            $("#votewrapper").unblock();
                            }   
                   });
        }
    })

祝你好运! :)

关于javascript - JQuery +thumbs_up gem 渲染投票数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6176392/

相关文章:

javascript - 在复选框选择上提交 Django 表单

javascript - ConstructorFunc.property 与 ConstructorFunc.prototype.property

jquery - IE 8 .png 问题

javascript - jquery 验证瑞典语字符

jquery - 如何使工具提示(qtip2)位于顶部中心?以及如何在其中添加图像?

ruby-on-rails - 如何在 Rails 中创建全局实用程序函数

javascript - 在firefox中读取xml文档

javascript - 扩展数组和闭包

ruby-on-rails - rails console 在将测试单元 gem 添加到 gemfile 时出现错误

ruby-on-rails - 选择关系不为空的记录