jQuery + thumbs_up gem渲染投票计数?

| 外挂程式: Thumbs Up&JQuery 1.5.2(需要另一个旧的gem) 当用户对帖子投票时,我正在尝试提供不带完整HTTP请求的更新的投票计数。目前,它会在每次投票时刷新页面。 职位控制器
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
投票视图(每个帖子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>
表决_up.erb.js(在岗位文件夹)。
$(\".votecount\").html(
\"<%= escape_javascript post.votes_for - post.votes_against %>\");
我已经坚持了一段时间,非常感谢您能提供的任何帮助。我已经看过Jquery的Railscast并浏览了其他Stackoverflow的答案,但是我对Jquery仍然不太满意。     
已邀请:
        似乎您想将视图代码分成多个部分,并且仅在提供评分时才刷新一个部分。 对于您的控制器,而不是:
respond_to do |format|
    format.js
    format.html {redirect_to :back}
  end
做类似的事情:
render :partial => \"voutecount\"
在您的视图中,将votwrapper div移出到同一目录中名为\“ _ votecount.html.erb \”的新文件中,而是使用呈现代码:
 <%= render :partial => \"votecount\" %>
如果您想在刷新时阻止评级(推荐),那么您可能希望取消调用并在js中对其进行更多控制。因此,在视图中包含您的JavaScript:
<%= javascript_include_tag \'votecount\' %>
用优质的html替换您的link_to以获取更多信息:
<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();
                            }   
                   });
        }
    })
祝好运! :)     

要回复问题请先登录注册