【发布时间】:2015-01-14 18:43:54
【问题描述】:
基于here 提供的有用且有效的解决方案,我也在尝试修复我的更新回调。
问题是,我试图从中提取数据的特定 unit 始终是旧的缓存版本,即使此回调是由成功的 update 操作触发的。
// callback triggered by the update action
$('.best_in_place').bind("ajax:success", function () {
...
console.log(unit.duration);
// which is exactly the same as
console.log(<%= Unit.find(unit.id).unit_users.pluck(:duration).sum %>);
// and both print the OLD duration val instead of the updated val which is in the database
});
还有 unit_users_controller 代码...
def update
@unit = @unituser.unit
respond_to do |format|
if @unituser.update(unit_user_params)
@unit.reload
logger.info('-----------------------------------------------------------------')
logger.info('@unit.duration in the controller is ' + @unit.duration.to_s) # which is the correct value
logger.info('-----------------------------------------------------------------')
gon.unit_duration = @unit.duration # an experiment which didn't work for me
format.json {respond_with_bip(@unituser) }
else
# format.html { render :action => 'edit' }
format.json { respond_with_bip(@unituser) }
end
end
end
我尝试了多个版本的unit.reload,但没有任何帮助。可能是我放错地方了?
【问题讨论】:
-
能贴出控制器代码吗?
-
@RodrigoZurek 与控制器代码无关。控制器与此无关。这是关于对
.js.erb文件如何工作的误解。 -
它与控制器有关,如果您想要更新的对象,它必须通过控制器并且您必须以某种方式将其取回
-
@RodrigoZurek 正确的解决方案是,是的。显示控制器代码无关紧要,因为控制器代码不是问题。问题是在视图中滥用
<%= %>。 -
粘贴在我的控制器代码中...
标签: ruby-on-rails best-in-place