我建立了一个简单的投票系统:
votes_controller.rb:
class VotesController < ApplicationController
def vote_up
@post = Post.find(params[:id])
@vote = @post.votes.create(:user_id => current_user.id, :polarity => 1)
end
end
(如果这里有任何不好的做法,请告诉我)
视图/show.html.erb:
<h3><%= @post.votes.count %> votes</h3><br />
<%= link_to "Vote Up", vote_up_path(@post), :remote => true %>
(当然我会把这个放在一部分)
路线.rb:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
当我单击“投票”链接时,投票会添加到帖子中,但我必须刷新页面才能看到更改。我怎样才能刷新@post.votes.count
and vote.user.username
动态地使用 Ajax/JavaScript?
首先为 show.html.erb 制作一个包装 div
<div class='post-<%[email protected]%>' >
<h3><%= @post.votes.count %> votes</h3><br />
<%= link_to "Vote Up", vote_up_path(@post), :remote => true %>
</div>
并在 vote_up.js.erb 中
$("post-<%[email protected]%>").html('<%=escape_javascript @post.votes.count %>');
或者类似的东西
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)