Ajax Rails 3如何使用递归循环(构建树形图)更新页面?

Ajax Rails 3如何使用递归循环(构建树形图)更新页面?,ajax,ruby-on-rails-3,model-view-controller,recursion,tree,Ajax,Ruby On Rails 3,Model View Controller,Recursion,Tree,我有以下情况 在模型中,我填充了一个表,该表具有树状结构。我有一个包含多个节点的图模型,以及一个包含“内容”列的节点模型,该列由以下内容填充: class Graph < ActiveRecord::Base def start_collecting self.content_collection(self.nodes.first) end def content_collection(root) root.children.all(:order =

我有以下情况

在模型中,我填充了一个表,该表具有树状结构。我有一个包含多个节点的图模型,以及一个包含“内容”列的节点模型,该列由以下内容填充:

class Graph < ActiveRecord::Base

   def start_collecting
    self.content_collection(self.nodes.first)
   end

   def content_collection(root)
    root.children.all(:order  => "idx DESC").each do |node|
    #Here the function in Node model is called, that populates 'content' column in node's table.
     content_array << 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  
在Graph.rb中:

def get_graph(root = self.nodes[0], word_ary = [])
  @root = root
  @root.children.all(:order  => "idx DESC").each do |child|
   n_node = (child.depth*2).to_s+child.content
   word_ary << 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

我做错了什么?再次感谢您的帮助。

让我确定我理解了,您想在图形增长时显示整个图形,每3秒钟刷新一次,对吗

我要做的第一件事是在GraphController中创建一个AJAX操作,以获取到目前为止图形的全部内容

def getstatus
  @graph_so_far = Graph.???  
  respond_to do |format|
    format_js
  end
end
当然,您必须添加一条新路线

然后在getstatus.js.erb中(与show.html.erb位于同一目录中)

var text=;
var pjs=Processing.getInstanceById(“graphsketch”);
pjs.update(文本);

这是您的第一次更新,现在您所要做的就是设置一个计时器,并在3秒钟后启动相同的AJAX调用,我使用jquery.timers.js,但无论如何,在3秒钟后,向URL/graph/getstatus发送另一个AJAX请求(或您称之为路由的任何内容),再次开始循环。

抱歉,您还必须进行初始AJAX调用才能开始,您只需向URL/graph/getstatus发送一个AJAX请求,可能是在DOM加载事件之后。谢谢!我现在就试试。。我不太明白-什么是“?”,你能解释一下吗?哦,我想是模特的电话。。我需要休息一下。我在问题中添加了更新的代码,你能看一下吗?getstatus.js.erb应该在app/views/graphs中,是吗?啊。实际上只是将format_js改为format.js,一切正常。谢谢!另外,我认为对AJAX的调用应该更像$.get(“/graphs/getstatus”);我总是使用jquery,所以我不太熟悉没有jquery的javascript!!!!哎呀,我的坏,对不起,这是我的一个常见问题!经常检查我发布的内容是否有明显的打字错误,等等。很高兴你正在运行!
def get_graph(root = self.nodes[0], word_ary = [])
  @root = root
  @root.children.all(:order  => "idx DESC").each do |child|
   n_node = (child.depth*2).to_s+child.content
   word_ary << 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
  <script>
 $(document).ready(function () {
  setInterval(function(){
    var idx  = "<%= @graph.id %>";
    $.get("getstatus/", {idx} )
      },1000);
 });      
  </script>
 <%= javascript_include_tag "pjs" %>
<canvas id="graphsketch" data-processing-sources="/assets/pjs/graphBuilder2_2.pde"></canvas>
 resource :graphs do
   collection do
     get 'getstatus'
   end
 end 
def getstatus
  @graph_so_far = Graph.???  
  respond_to do |format|
    format_js
  end
end
var text = <%= array_or_string_for_javascript(@graph_so_far)%> ;
var pjs = Processing.getInstanceById("graphsketch");
pjs.update(text);