【问题标题】:Reddit-style nested/threaded/indented comments for Rails?Rails 的 Reddit 样式的嵌套/线程/缩进注释?
【发布时间】:2010-10-02 14:41:21
【问题描述】:

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

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

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

或者也许有一个包含此功能的开源项目。

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

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

【问题讨论】:

    标签: ruby-on-rails reddit threaded-comments acts-as-tree


    【解决方案1】:

    您是否在您的模型上尝试过acts_as_tree 插件? 这是一个官方的 ActiveRecord 组件。

    http://wiki.rubyonrails.org/rails/pages/ActsAsTree

    【讨论】:

      【解决方案2】:

      使用acts_as_tree 插件应该可以很容易地实现。使用安装它

      ruby script/plugin install acts_as_tree

      app/models/comment.rb

      class Comment < ActiveRecord::Base
        acts_as_tree :order => 'created_at'
      end
      

      db/migrate/20090121025349_create_comments.rb

      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
      

      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>
      

      app/views/comments/show.html.erb

      <div id="comments">
        <%= render :partial => 'comments/comment', :object => Comment.find(params[:id]) %>
      </div>
      

      魔法发生在show.html.erb 调用&lt;%= render :partial =&gt; 'comments/comment', :object =&gt; Comment.find(params[:id]) %&gt; 时,这将导致部分递归渲染所有子cmets。如果你想限制深度,可以在局部或模型中进行。

      编辑:
      这将使您在 HTML 中为每个深度都提供相同间距的所有 cmets。如果您想生成易于阅读的 HTML,只需使用 render(...).gsub(/^/, "\t") 这将递归地工作以及生成良好缩进的 HTML。

      我在app/helpers/application_helper.rb中将它组合成我自己的方法

      def indented_render(num, *args)
        render(*args).gsub(/^/, "\t" * num)
      end
      

      所以现在你可以拨打&lt;%= indented_render 1, :partial =&gt; 'comments/comment', ... %&gt;

      编辑:
      修复了示例中缺少关闭 &lt;/h1&gt; 标记的问题。

      【讨论】:

      • 我相信您在评论标题中缺少结束 h1。除此之外,很棒的答案!谢谢! :)
      • 天哪!很好的答案!这真的帮助我完成了一个项目。我从没想过使用acts_as_tree。
      • 我正在尝试使用acts_as_tree,但我无法让indented_render 工作。 this 是我正在做的事情。我做错了什么?
      • 当您尝试抓取 cmets 时,这会创建多少条 sql 语句?这种方法是否可扩展?
      【解决方案3】:

      Hector 和 Samuel 提供的 ActsAsTree 文档链接似乎已损坏。您可以在

      处获取文档 http://web.archive.org/web/20061011101510/http://wiki.rubyonrails.org/rails/pages/ActsAsTree

      (我使用了一个前置标签,因为链接由于某种原因一直显示错误)。

      【讨论】:

        【解决方案4】:

        有一个 has_threaded_cmets gem,从未使用过,但它看起来正是这样做的: https://github.com/aarongough/has_threaded_comments

        【讨论】:

          猜你喜欢
          • 2011-08-20
          • 1970-01-01
          • 2014-11-27
          • 2011-10-03
          • 1970-01-01
          • 2011-05-17
          • 2015-01-27
          • 2011-07-12
          • 2011-10-11
          相关资源
          最近更新 更多