【发布时间】:2016-08-02 04:08:47
【问题描述】:
我正在使用 Railscast 教程来实现 activerecord 信誉系统:http://railscasts.com/episodes/364-active-record-reputation-system?view=comments
但用户投票后我无法隐藏按钮。
我是这样做的:
后控制器:
def accept
value = params[:type] == "accept" ? 1 : -1 # vote type = accept
@improvement_action = ImprovementAction.find(params[:id])
@improvement_action.add_or_update_evaluation(:accepts, value, current_user)
redirect_to :back, notice: "You accepted the answer!"
end
后模型:
has_reputation :accepts, source: :user, aggregated_by: :sum
用户模型:
has_many :evaluations, class_name: "RSEvaluation", as: :source #accept answer (activerecord reputation-system gem)
has_reputation :accepts, source: {reputation: :accepts, of: :improvement_actions}, aggregated_by: :sum
def voted_for? improvement_action
evaluations.where(target_type: improvement_action.class, target_id: improvement_action.id).exists?
end
并在视图中这样调用:
<% if current_user && current_user.voted_for?(improvement_action) %>
VOTED
<% else %>
NOT VOTED
<%= link_to "up", accept_improvement_action_path(improvement_action, type: "accept"), method: "post" %>
<% end %>
我做错了什么?像这样似乎总是被投票
【问题讨论】:
-
如果用户没有投票则显示链接和
Voted?你的 if 条件倒置了吗? -
是的,没错,我换了我的错误。但是问题是一样的,因为不管我投不投,看起来都是一样的
-
我怀疑
add_or_update_evaluation(:accepts, value, current_user)不起作用。 -
我认为您在此处的视图文件中缺少 if 条件,您可以重新发布吗?
-
是的,它没有出现,我编辑了
标签: ruby-on-rails ruby activerecord