我有这段代码在我的rails应用程序中实现了拇指up/down
功能
if params[:vote][:type] == "up"
answer = Answer.find_by_id(params[:vote][:id])
answer.increment(:ups)
render text: answer.ups
end
if params[:vote][:type] == "down"
answer = Answer.find_by_id(params[:vote][:id])
answer.increment(:downs)
render text: answer.downs
end
它不会产生任何错误,但增量值不会在数据库中更新。
此代码在rails控制台中正常运行。
请帮忙,
提前致谢
答案 0 :(得分:3)
这是by design。该方法递增属性但不保存。如果您想要保存,请使用increment!
(bang version)。
答案 1 :(得分:1)
检查一次
if params[:vote][:type] == "up"
answer = Answer.find_by_id(params[:vote][:id])
answer.to_i.increment(:ups)
render text: answer.ups
elsif params[:vote][:type] == "down"
answer = Answer.find_by_id(params[:vote][:id])
answer.to_i.increment(:downs)
render text: answer.downs
end
答案 2 :(得分:1)
另一次迁移:
change_column :answers, :ups, :integer, :default: 0
change_column :answers, :downs, :integer, :default: 0
可能会帮到你