1

Been stuck on this for a while now, and My JavaScript is very limited! Have a book on order but for now i am stuck.

I am trying to refresh a Partial on my index page every 3 seconds with Jquery and AJAX.

# agents_controller
def refresh_partial
        render :partial => "agents/dynamic"
end


#the js
<script type="text/javascript">         
$(document).ready(
     function() {
      setInterval(function() {
        $('.dynamic').load('/agents/refresh_partial');
    }, 3000);
});
</script>

But all i get is

stack level too deep

but no stack trace.

My guess is that is is calling the partial infinitely, but i cant see how or why?

ANy suggestions? Im very new at all this but learning fast, with a book in each hand and really appreciate you guys' support!

Pulling from the answer in this question Reloading partial in an rails app but it is not working for me.

For Completness:

 #/agents/index

 <div class="dynamic"><%= render partial: 'dynamic' %></div>

 #/agents/_dynamic
 <div class="span4">
 <table class="table table-striped" > 


<% if @ttb.nil? %>
<% else %>
<% @ttb.each_slice(3) do |elem| %>
<tr>
<td><%  elem.each_slice(3) do |a,b,c| %></td>
<td><%=  a.gsub(/[^0-9A-Za-z]/, '') %></td>
<td><div id="content"><%=  b.gsub(/[^0-9A-Za-z]/, '') %></div></td>
<td><%=  c.gsub(/[^0-9A-Za-z]/, '').scan(/.{2}|.+/).join(":") %></td>
</tr>

<% end %>
<% end %>
<% end %>

</table>
</div>

Can anyone see what a cocked up or misunderstood?

Many Thanks

Community
  • 1
  • 1
IT_puppet_master
  • 702
  • 5
  • 20

2 Answers2

1

I used to do this with ajax, because you need to refresh your @ttb.

So, the script:

$(document).ready(
    function() {
        setInterval(function() {
            $.ajax({
                url: '<%= controller_path(params) %>',
                type: 'GET',
                data: {},
                dataType: 'script',
            });
        }, 3000);
    });

On controller:

# agents_controller
def refresh_partial

    @ttb = Class.all

    respond_to do |format|
        format.js
    end
end

Make a new file on view folder: refresh_partial.js.erb with:

$('.dynamic').html("<%= escape_javascript('render 'dynamic', ttb: @tb) %>");

This will rewrite the div with the partcial.

Other tip, dont use @ttb on partial, send the ttb when you call the render, like i did, remenber to fix the partial code.

  • Excellent, thats starting to look the part. Why not use @ttb out of interest? Just trying it now! Will let you know. Thanks – IT_puppet_master Feb 28 '14 at 11:30
  • Because @ttb its like a global var to views and partials, if he change, just send a new one to the partial. – Leandro Falcão Feb 28 '14 at 11:36
  • Well, it is polling the server now, but rendering a blank partial. So I am not passing it variables correctly i guess – IT_puppet_master Feb 28 '14 at 11:40
  • Did you make the respond_to file? and can you debug the respond from ajax?, make what toxaq told, change class dinamic to an id. – Leandro Falcão Feb 28 '14 at 11:41
  • refresh_partial.js.erb? Yep – IT_puppet_master Feb 28 '14 at 11:43
  • So, look on debug, if the ajax call the right controller, and the response. – Leandro Falcão Feb 28 '14 at 11:46
  • Sorry to be a pain, and i hate being spoon fed, but what should my partial now look like, cause its rendering blank still, but cant see any errors? – IT_puppet_master Feb 28 '14 at 11:49
  • Did you update your var on controller?, write something after @ttb.nil? to look if its null. If you change the var like i say, now use ttb on partial (not @ttb), and remember to send it on call. – Leandro Falcão Feb 28 '14 at 11:51
  • Its still how i am passing variables to the partail. Firebug says its getting the page no problems, inspecting the HTML it gets shows its the same each time. So the partial is refreshing on the screen, but the content is not refreshing in the partial. Sorry, but im really confused with these VAR´s now, and how and what to pass when to get it to refresh. – IT_puppet_master Feb 28 '14 at 12:02
  • heheh, sry, now i'm confuse too, the html on response on firebug its correct? the partial update on screen? now the problem its only when u change @ttb? – Leandro Falcão Feb 28 '14 at 12:09
  • exactly, Firebug shows its doing the refresh, as does my development server log, but the HTML that firebug is showing for the refresh is the same every time. Even if i add content to the partail manually while its running, it does not update, Still pulls the same HTML for the partail, untill i het page refresh, when it will update it – IT_puppet_master Feb 28 '14 at 12:19
  • go to chat: http://chat.stackoverflow.com/rooms/5676/ruby-on-rails i will get some coffe now, 5mim and i will be back – Leandro Falcão Feb 28 '14 at 12:21
  • Thankyou! I get some coffe too and be with you in 5! :) – IT_puppet_master Feb 28 '14 at 12:32
0

Is your JS snippet by any chance inside the ´agents/dynamic´ partial? If so, the function is somewhat executing recursive, with each load of the partial executing an additional function also loading the very same partial (and so on...)

You should extract the JS to the surrounding page (´agents/index´) or use the ´setTimeout()´ method instead, which only executes once.

If the JS is already on the index page, then maybe the reload of the partial also triggers the $(document).ready again. Have you tried debugging it with FireBug or similar browser plugins? Then you could see why it's executing more often than you intended.

KappaNossi
  • 2,656
  • 1
  • 15
  • 17
  • The JavaScript is in the tags of index page, not the partial. Have also tried putting it in application.js and calling it, but nothing works. JavaConsole shows it subbmiting requests to the server but cant see the code its executing. Will try Firebug now. Thanks – IT_puppet_master Feb 28 '14 at 10:44