它只发生在生产中,当我们通过浏览器更新一些记录时,更改没有保存。看来不是缓存的问题,因为我们验证了mysql中的数据还是旧数据。然而, Controller 确实被击中并返回了闪现消息,就好像更改已成功完成一样。
但是,我们可以在 rails console 或 mysql 中手动进行更改,没有任何问题。
知道为什么会这样吗?
顺便说一句,我们最近重新配置站点以使用 SSL,这可能与此有关。
最佳答案
是否有任何东西可以阻止模型被保存?
确保设置属性并保存模型的一种方法是使用异常引发版本,它可以帮助解决如下问题:
def update
@model = Model.find(params[:id])
@model.update_attributes(params[:model])
redirect_to(model_path(@model))
end
这可以改进为更可靠的方法:
def update
@model = Model.find(params[:id])
# Use exception-throwing update_attributes!
@model.update_attributes!(params[:model])
redirect_to(model_path(@model))
rescue ActiveRecord::RecordNotFound
render(:partial => 'not_found')
rescue ActiveRecord::RecordInvalid
# Delegate back to edit action, something's invalid
edit
render(:action => 'edit')
end
有些情况下 update_attributes 可能无法成功保存。
如果您可以使用相同的方法对相同的数据执行相同的更新,那就很奇怪了。
关于mysql - Rails update_attribute 没有命中 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1550974/