我有这段代码可以在我的 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 控制台中正常运行。
请帮忙,
提前致谢
最佳答案
这是 by design .该方法递增属性但不保存。如果您还想保存,请使用 increment!
( bang version)。
关于ruby-on-rails - 为什么 Rails 增量方法不保存更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16937377/