【发布时间】:2012-01-11 08:43:11
【问题描述】:
我正在尝试使用 thumbs_up gem 显示帖子的总票数,但它似乎不起作用。
这是我的代码
def vote_up
begin
post = Post.find(params[:id])
current_user.vote_for(post)
redirect_to :back
flash[:sucess] = "You have voted successfully"
@votes_total = post.votes_for
rescue ActiveRecord::RecordInvalid
redirect_to :back
flash[:error] = "You have already voted for this one"
end
end
在视图中:-
<%="Total votes = #{@votes_total}"%>
我收到“您已成功投票”的 Flash 消息,但我的投票数未显示。
这是我的日志文件中的内容:-
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM "votes" WHERE "votes"."voteable_id" = 12 AND "votes"."voteable_type" = 'Post' AND "votes"."vote" = 't'[0m 0
--- 更新---
使用此代码更新我的后控制器:-
def vote_up
begin
post = Post.find(params[:id])
current_user.vote_for(post)
@votes_total = post.votes_for
render :template => "home/index"
flash[:sucess] = "You have voted successfully"
rescue ActiveRecord::RecordInvalid
redirect_to :back
flash[:error] = "You have already voted for this one"
end
end
请帮忙。
【问题讨论】:
标签: ruby-on-rails-3 ruby-on-rails-3.1 voting