Rails 的 Reddit 样式嵌套/线程/缩进注释? [英] Reddit-style nested/threaded/indented comments for Rails?

查看:46
本文介绍了Rails 的 Reddit 样式嵌套/线程/缩进注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果有人能指出我执行此操作的代码,将不胜感激.

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

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

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

解决方案

使用 acts_as_tree 插件应该使这相当容易实现.使用

安装

ruby 脚本/插件安装acts_as_tree

app/models/comment.rb

class 注释 'created_at'结尾

db/migrate/20090121025349_create_comments.rb

class CreateComments 

app/views/comments/_comment.html.erb

<h1><%=comment.title%></h1><%=评论.内容%><%=渲染:部分=>'评论/评论', :collection =>评论.儿童 %>

app/views/comments/show.html.erb

<%=渲染:部分=>'评论/评论', :object =>Comment.find(params[:id]) %>

魔法发生在 show.html.erb 中,当它调用 <%= render :partial =>;'评论/评论', :object =>Comment.find(params[:id]) %>,这将导致部分递归呈现所有子评论.如果要限制深度,可以在局部或模型中进行.


这将使您在 HTML 中为每个深度留下具有相同间距的所有注释.如果您想生成易于阅读的 HTML,只需使用 render(...).gsub(/^/, "\t") 这将递归工作以及生成缩进良好的 HTML.
>

我在app/helpers/application_helper.rb

中将它组合成我自己的方法

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

所以现在你可以调用 <%= indented_render 1, :partial =>'评论/评论', ... %>


修复了示例中缺少的结束 </h1> 标记.

I'm wondering if someone has already built a system for threaded comments (for lack of a better term) in Rails or if I need to build it myself.

In case it's not clear, what I'm referring to is a comment system like Reddit's that automatically indents replies so that they appear like branches of a tree (preferably with voting just as Reddit does).

If someone could point me to code that does this, it would be greatly appreciated.

Or perhaps there is an open source project that includes this functionality.

So far I have not been able to find one in Rails.

Also, would it be better to ask this on a Rails forum and, if so, which one? (I'm new to Rails)

解决方案

Using the acts_as_tree plugin should make this fairly easy to implement. Install it using

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>

The magic happens in show.html.erb when it calls <%= render :partial => 'comments/comment', :object => Comment.find(params[:id]) %>, this will cause the partial to recursively render all children comments. If you want a limit to the depth, you can do it in the partial or in the model.

Edit:
This will leave you with all the comments with the same spacing in the HTML for every depth. If you want to produce easy to read HTML, just use render(...).gsub(/^/, "\t") this will work recursively as well producing nicely indented HTML.

I combined it into my own method in app/helpers/application_helper.rb

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

So now you can call <%= indented_render 1, :partial => 'comments/comment', ... %>

Edit:
Fixed missing closing </h1> tag in the example.

这篇关于Rails 的 Reddit 样式嵌套/线程/缩进注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆