Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 how do I update the page with a recursive loop (building tree graph)?
    primarykey
    data
    text
    <p>I have a following situation.</p> <p>In the model I am populating a table, which has a tree-like structure. I have a Graph model that has_many :nodes and Node model that has a "content" column that is populated during by the following:</p> <pre><code>class Graph &lt; ActiveRecord::Base def start_collecting self.content_collection(self.nodes.first) end def content_collection(root) root.children.all(:order =&gt; "idx DESC").each do |node| #Here the function in Node model is called, that populates 'content' column in node's table. content_array &lt;&lt; node.content.to_s self.children_content = content.array self.save! child.collect_content if !node.children.blank? self.content_collection(node) end end end </code></pre> <p>The question is.. I want to have the progress being displayed on the web page (app/views/graphs/show.html.erb). So far I am doing this:</p> <pre><code> setInterval(function(){ var text = &lt;%= array_or_string_for_javascript(@graph.children_content)%&gt; ; var pjs = Processing.getInstanceById("graphsketch"); pjs.update(text); }, 3000); </code></pre> <p>So that I am just redrawing the content of @graph.children_content on screen every 3 seconds.. I wonder what would be the best way to draw the contents of each @node.content with this timed refresh? Obviously if I just write an .each loop in show.html.erb I will keep getting the content of the last @node in the loop... Basically I wonder how to translate the loop from the above model onto the view and make the progress visible?</p> <p>Hope this is clear enough, please let me know if I should explain better... </p> <p>EDIT :: </p> <p>So following the advice, now I have in graph_controller.rb:</p> <pre><code>def getstatus @graph = Graph.find(params[:id]) @graph_so_far = @graph.get_graph respond_to do |format| format_js end end </code></pre> <p>in Graph.rb:</p> <pre><code>def get_graph(root = self.nodes[0], word_ary = []) @root = root @root.children.all(:order =&gt; "idx DESC").each do |child| n_node = (child.depth*2).to_s+child.content word_ary &lt;&lt; n_node child.parent_relation.collect_videos_multiple_cats if !child.children.blank? self.get_graph(child, word_ary) end end return word_ary end </code></pre> <p>In graphs/show.html.erb :</p> <pre><code> &lt;script&gt; $(document).ready(function () { setInterval(function(){ var idx = "&lt;%= @graph.id %&gt;"; $.get("getstatus/", {idx} ) },1000); }); &lt;/script&gt; &lt;%= javascript_include_tag "pjs" %&gt; &lt;canvas id="graphsketch" data-processing-sources="/assets/pjs/graphBuilder2_2.pde"&gt;&lt;/canvas&gt; </code></pre> <p>In graphs/getstatus.js.erb</p> <p>var text = &lt;%= array_or_string_for_javascript(@sentence_so_far)%> ; var pjs = Processing.getInstanceById("graphsketch"); alert(text); pjs.update(text);</p> <p>The alert in getstatus.js.erb never fires.</p> <p>In routes (of course):</p> <pre><code> resource :graphs do collection do get 'getstatus' end end </code></pre> <p>What am I doing wrong? Thanks a lot for the help again..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload