嗨,我完全被这件事缠住了,正在寻求帮助
当我在我的对象simulation上做一个显示时,我希望一些javascript每十秒钟开始轮询一次,异步调用simulation\update
我想通过响应来实现这一点,例如:
def show
@simulation=simulation.find(参数[:id])
回应待办事项|格式|
format.js
format.html{redirect_to simulation_url}#这会导致问题
终止
终止
因此,我会有一个update.js.erb,它可以做一些类似的事情(对不起,咖啡脚本)
$.ajax({
键入:“放置”,
url:“/simulations/#{params}”
})
$(“#sim_div').html(<;%=j([email protected])%>;”;
设置超时(callUpdate,10000)
回来
如果我包含format.htmljavascript没有运行,并且我得到了一个格式错误,并且如果我包含了该行,那么我会得到一个未知的格式错误,那么我就无法调用这个javascript部分
正确的方法是什么?我在资产管道中尝试了大量使用coffeescript的解决方案,并且没有adue,而是使用了怪异的includes和内联javascript
为清楚起见,我的观点是:
<;%=渲染“模拟”%>;
<;%=链接到“返回”,模拟路径%>;
脚本和视图加载的部分内容是:
<;div id=“sim\u div”>;
<;h1><;%[email protected]%></h1>;
<;h4 class=“offset-col-sm-1”>;维度:<;%[email protected]模拟x_尺寸%>;x<;%[email protected]模拟。y_尺寸%></h4>;
<;h4 class=“offset-col-sm-1”>;裁决:<;%[email protected]%></h4>;
<;table class=“table table bordered”>;
<;thead>;
<;tr>;
</tr>;
</thead>;
<;t车身>;
<;%@simulation.state.each do | row |%>;
<;tr>;
<;%行。每个do |当前|%>;
<;td class=“文本中心”><;%=当前%></td>;
<;%结束%>;
</tr>;
<;%结束%>;
</t车身>;
</表>;
<;br>;
</部门>;
你可以尝试一个快捷方式,比如respond\u to:html,:js,看看这是否会清除它。否则,请尝试更明确的方法:
def show
@simulation=simulation.find(参数[:id])
回应待办事项|格式|
format.js{render:“某些视图”}
format.html{redirect_to simulation_url}
终止
终止
:show操作中的format.js在使用ajax调用show时,默认情况下将呈现show.js.erb。html响应将是重定向
这篇博客文章可能会有所帮助