Quantico
Quantico

Reputation: 2436

Ajax to reload div content in Rails 4 error + where to put remote: true

I am trying to periodically refresh a div on my screen. the div is in my index.html.erb and looks like this

<div id="shared_tasks_div">
<ul>
  <% @canEditTasks.each do |elem| %>
     <div class="draggable">    
     some code to put elem values in <p>
     </div>
  <% end %>
    <% @canView.each do |elem| %>
    <div class="draggable">    
     some code to put elem values in <p>
    </div>
<% end %>
</ul>
</div>

in my task controller I added the following code

  def index
    @tasks = current_user.task
    @canEditTask = some query
    @canView = another query
    respond_to do |format|
      format.js { render :div_tasks }
    end
  end

I created the following js div_tasks.js.erb in my app/views/tasks

$("#shared_tasks_div").html("<%= j(render('index', :canEditTasks=> @canEditTasks)) %>");
$("#shared_tasks_div").html("<%= j(render('index', :canView=> @canView)) %>");

I now have to issue a) where should I put the remote: true b) I am getting the following error from my index method

ActionController::UnknownFormat
    respond_to do |format|
      format.js { render :div_task }

I did see the following two posts and integrated some of the suggestions to my solution, but it did not solve the issue Rails 4 ajax update div Rails 3 - Update div content with Ajax and jquery (nested resources)

thank you for your help

Upvotes: 2

Views: 1359

Answers (1)

Moran Bentzur
Moran Bentzur

Reputation: 151

Try adding a periodic javascript ajax call to your index.html.erb file. The javascript should include an ajax call (dataType: script will insure that format.js) to the index method in your task controller.

You can use the methods outlined here: How to fire AJAX request Periodically?

Upvotes: 1

Related Questions