Rails中的respond_to出现未知格式错误。正确地尝试实施轮询更新

问题描述 投票:10回答:1

嗨,我完全坚持这一点,并寻求一些帮助。

当我在我的对象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

所以我会有一个update.js.erb做的事情(抱歉coffeescript)

$.ajax({
    type: "PUT",
    url: "/simulations/#{params}"
}) 

$('#sim_div').html("<%= j (render @simulation) %>");

setTimeout(callUpdate, 10000)
return

我不能得到这个javascript部分被调用,如果我包括format.html javascript没有运行,我得到格式错误,如果我确实包括该行,那么我得到一个未知的格式错误。

什么是正确的方法来解决这个问题?我已经在资产管道中尝试了大量使用coffeescript的解决方案,并且奇怪的包含和内联javascript没有任何问题。

为清楚起见,我的观点是:

<%= render 'simulation' %>

<%= link_to 'Back', simulations_path %>

以及脚本和视图加载的部分是:

<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>
javascript ruby-on-rails ajax view controller
1个回答
0
投票

您可以尝试像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

format.js动作中的:show默认情况下会在使用ajax调用show时呈现show.js.erb。 html响应将是重定向。

This博客文章可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.