Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails的Reddit样式嵌套/线程化/缩进注释?_Ruby On Rails_Reddit_Threaded Comments_Acts As Tree - Fatal编程技术网

Ruby on rails Rails的Reddit样式嵌套/线程化/缩进注释?

Ruby on rails Rails的Reddit样式嵌套/线程化/缩进注释?,ruby-on-rails,reddit,threaded-comments,acts-as-tree,Ruby On Rails,Reddit,Threaded Comments,Acts As Tree,我想知道是否有人已经在Rails中构建了一个线程评论系统(因为没有更好的术语),或者我是否需要自己构建它 如果不清楚,我指的是像Reddit这样的评论系统,它会自动缩进回复,使它们看起来像树的分支(最好像Reddit一样进行投票) 如果有人能给我指出这样做的代码,我将不胜感激 或者可能有一个开源项目包含此功能 到目前为止,我还没有在Rails中找到一个 另外,在Rails论坛上问这个问题会更好吗?如果是,是哪一个?(我是Rails新手)您是否在您的模型上尝试了acts\u as\u tree插件

我想知道是否有人已经在Rails中构建了一个线程评论系统(因为没有更好的术语),或者我是否需要自己构建它

如果不清楚,我指的是像Reddit这样的评论系统,它会自动缩进回复,使它们看起来像树的分支(最好像Reddit一样进行投票)

如果有人能给我指出这样做的代码,我将不胜感激

或者可能有一个开源项目包含此功能

到目前为止,我还没有在Rails中找到一个


另外,在Rails论坛上问这个问题会更好吗?如果是,是哪一个?(我是Rails新手)

您是否在您的模型上尝试了
acts\u as\u tree
插件? 它是一个官方的ActiveRecord组件

使用该插件应该使这一点相当容易实现。使用

ruby脚本/插件安装充当树

app/models/comment.rb

class Comment < ActiveRecord::Base
  acts_as_tree :order => 'created_at'
end
class CreateComments < ActiveRecord::Migration
  def self.up
    create_table :comments do |t|
      t.references :parent
      t.string :title
      t.text :content
      ...
      t.timestamps
    end
  end

  def self.down
    drop_table :comments
  end
end
def indented_render(num, *args)
  render(*args).gsub(/^/, "\t" * num)
end
app/views/comments/_comment.html.erb

<div id="comment_<%= comment.id %>">
  <h1><%= comment.title %></h1>
  <%= comment.content %>
  <%= render :partial => 'comments/comment', :collection => comments.children %>
</div>
<div id="comments">
  <%= render :partial => 'comments/comment', :object => Comment.find(params[:id]) %>
</div>
当调用
'comments/comment',:object=>comment.find(params[:id])%>
时,神奇的事情发生在
show.html.erb
中,这将导致分部递归地呈现所有子注释。如果要限制深度,可以在局部或模型中进行

编辑:
这将在HTML中为每个深度留下具有相同间距的所有注释。如果您想生成易于阅读的HTML,只需使用
render(…).gsub(/^/,“\t”)
即可递归生成缩进良好的HTML

我在
app/helpers/application\u helper.rb中将它合并到我自己的方法中

class Comment < ActiveRecord::Base
  acts_as_tree :order => 'created_at'
end
class CreateComments < ActiveRecord::Migration
  def self.up
    create_table :comments do |t|
      t.references :parent
      t.string :title
      t.text :content
      ...
      t.timestamps
    end
  end

  def self.down
    drop_table :comments
  end
end
def indented_render(num, *args)
  render(*args).gsub(/^/, "\t" * num)
end
因此,现在您可以调用
“comments/comment”,…%>

编辑:

修复了示例中缺少的结束标记。

Hector和Samuel提供的ActsAtree文档链接似乎已断开。你可以在

http://web.archive.org/web/20061011101510/http://wiki.rubyonrails.org/rails/pages/ActsAsTree http://web.archive.org/web/20061011101510/http://wiki.rubyonrails.org/rails/pages/ActsAsTree
(我使用了一个pre标记,因为链接由于某种原因一直显示错误)。

有一个has\u threaded\u comments gem,从未使用过,但它看起来确实是这样做的:

我相信您在评论标题中遗漏了一个结尾h1。除此之外,这个回答太棒了!谢谢!:)天哪!回答得好!这真的帮了我一个项目。我从来没有想过使用acts_as_tree。我正在尝试使用acts_as_tree,但我无法使用缩进渲染。这就是我正在做的。我做错了什么?当您试图获取注释时,这会创建多少sql语句?这种方法是可伸缩的吗?