NNN*_*ous 11 javascript ajax controller ruby-on-rails view
嗨,我完全坚持这一点,并寻求一些帮助.
当我在我的对象上做一个节目时,我simulation想要一些javascript每隔十秒开始轮询,以simulation#update异步方式调用.
我希望这样做respond_to:
def show
@simulation = Simulation.find(params[:id])
respond_to do |format|
format.js
format.html { redirect_to simulation_url } # This causes problems
end
end
Run Code Online (Sandbox Code Playgroud)
所以我会update.js.erb做一些事情(对于coffeescript抱歉)
$.ajax({
type: "PUT",
url: "/simulations/#{params}"
})
$('#sim_div').html("<%= j (render @simulation) %>");
setTimeout(callUpdate, 10000)
return
Run Code Online (Sandbox Code Playgroud)
我不能得到这个javascript部分被调用,如果我包括format.htmljavascript没有运行,我得到一个格式错误,如果我确实包括该行,那么我得到一个未知的格式错误.
什么是正确的方法来解决这个问题?我已经在资产管道中尝试了大量使用coffeescript的解决方案,并且奇怪的包含和内联javascript没有任何问题.
为清楚起见,我的观点是:
<%= render 'simulation' %>
<%= link_to 'Back', simulations_path %>
Run Code Online (Sandbox Code Playgroud)
以及脚本和视图加载的部分是:
<div id="sim_div">
<h1><%= @simulation.identifier %></h1>
<h4 class="offset-col-sm-1">Dimensions: <%= @simulation.x_size %>x<%= @simulation.y_size %></h4>
<h4 class="offset-col-sm-1">Verdict: <%= @simulation.verdict %></h4>
<table class="table table-bordered">
<thead>
<tr>
</tr>
</thead>
<tbody>
<% @simulation.state.each do |row| %>
<tr>
<% row.each do |current| %>
<td class="text-center"><%= current %></td>
<% end%>
</tr>
<% end %>
</tbody>
</table>
<br>
</div>
Run Code Online (Sandbox Code Playgroud)
您可以尝试类似的快捷方式respond_to :html, :js,看看是否可以解决问题。否则尝试更明确的东西:
def show
@simulation = Simulation.find(params[:id])
respond_to do |format|
format.js { render: "some view"}
format.html { redirect_to simulation_url }
end
end
Run Code Online (Sandbox Code Playgroud)
format.js当使用 ajax 调用 show 时,该:show操作将默认呈现。show.js.erbhtml 响应将是重定向。
这篇博文可能会有所帮助。