插件: 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>
vote_up.erb.js(在帖子文件夹中)。
$(".votecount").html(
"<%= escape_javascript post.votes_for - post.votes_against %>");
我已经坚持了一段时间,非常感谢你能提供的任何帮助。我已经看过 Jquery railscast 并查看了其他 Stackoverflow 答案,但我对 Jquery 仍然很陌生。