Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown Format error with respond_to in Rails. Correctly trying to implementing polling-to-update

Hi I'm totally stuck on this and looking for some help.

When I do a show on my object simulation I want some javascript to start polling every ten seconds, to call simulation#update asynchronously.

I want to do this by respond_to, as such:

def show
    @simulation = Simulation.find(params[:id])
    respond_to do |format|
        format.js
        format.html { redirect_to simulation_url } # This causes problems
    end
end

So I would have a update.js.erb that does something along the lines of (sorry for the coffeescript)

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

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

setTimeout(callUpdate, 10000)
return

I can't get this javascript partial to be called if I include the format.html the javascript doesnt get run and I get a format error, and if I do include that line, then I get a unknown format error.

Whats the correct way to go about this? I've tried tons of solutions using coffeescript in the asset pipeline and weird includes and inline javascript to no adue.

For clarity my view is:

<%= render 'simulation' %>

<%= link_to 'Back', simulations_path %>

and the partial that the script and view loads is:

<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>
like image 480
NNNNNNNNNNDelicious Avatar asked Aug 12 '15 02:08

NNNNNNNNNNDelicious


2 Answers

You can try a shortcut like respond_to :html, :js and see if that clears it up. Otherwise try something more explicit:

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 in the :show action will by default render show.js.erb when show is called with ajax. The html response will be a redirect.

This blog post may be helpful.

like image 109
Chase Avatar answered Nov 16 '22 02:11

Chase


Perhaps issue is with whatever calls the show action in the first place, and it's missing the remote: true option ?

If its a link then try with remote: true option, something like :

<%= link_to 'Show', simulation_path(my_simulation), remote: true %>

same if its a button

<%= button_to 'Show', simulation_path(my_simulation), remote: true %>

This should cause Rails to call the controller with correct format and in logs you should see JS mentioned, e.g.

Processing by SimulationsController#show as JS

Also in the partial you are probably missing the dataType, try :

$.ajax({
    type: "PUT",
    url: "/simulations/#{params}",
    dataType: "script"
})
like image 1
aqwan Avatar answered Nov 16 '22 04:11

aqwan