标签: acts-as-tree

Rails的Reddit样式嵌套/线程/缩进注释?

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

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

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

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

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

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

ruby-on-rails reddit acts-as-tree threaded-comments

14
推荐指数
1
解决办法
7221
查看次数

Formtastic选择分组

现在使用Formtastic我可以选择:

= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
Run Code Online (Sandbox Code Playgroud)

这里我只展示儿童类别.我使用acts_as_tree插件进行父子关系.我想展示父类别.

Formtastic生成的选择应该如下所示:

<select name="favoritefood">
  <optgroup label="Dairy products">
    <option>Cheese</option>
    <option>Egg</option>
  </optgroup>
  <optgroup label="Vegetables">
    <option>Cabbage</option>
    <option>Lettuce</option>
    <option>Beans</option>
    <option>Onions</option>
  <option>Courgettes</option>
  </optgroup>
  ?
</select>
Run Code Online (Sandbox Code Playgroud)

如何在Formtastic中使用分组选择具有acts_as_tree功能的模型?有人知道吗?

更新

我发现这应该有效:

= f.input :category, :include_blank => false, :group_by => :parent
Run Code Online (Sandbox Code Playgroud)

但它没有错误:

undefined local variable or method `object_class' for #<Formtastic::SemanticFormBuilder:0x87d3158>
Run Code Online (Sandbox Code Playgroud)

看起来Formtastic中有一些错误.我查看了formtastic.rb并在detect_group_association方法中找到了object_class :

  def detect_group_association(method, group_by)
    object_to_method_reflection = self.reflection_for(method)
    method_class = object_to_method_reflection.klass

    method_to_group_association = method_class.reflect_on_association(group_by)
    group_class = method_to_group_association.klass

    # This will …
Run Code Online (Sandbox Code Playgroud)

grouping ruby-on-rails acts-as-tree optgroup formtastic

8
推荐指数
1
解决办法
9233
查看次数

acts_as_tree不会破坏模型的子项

我有这个任务模型:

class Task < ActiveRecord::Base
  acts_as_tree :order => 'sort_order'
end
Run Code Online (Sandbox Code Playgroud)

我有这个测试

class TaskTest < Test::Unit::TestCase
  def setup
    @root = create_root
  end

  def test_destroying_a_task_should_destroy_all_of_its_descendants
    d1 = create_task(:parent_id => @root.id, :sort_order => 2)
    d2 = create_task(:parent_id => d1.id, :sort_order => 3)
    d3 = create_task(:parent_id => d2.id, :sort_order => 4)
    d4 = create_task(:parent_id => d1.id, :sort_order => 5)
    assert_equal 5, Task.count

    d1.destroy

    assert_equal @root, Task.find(:first)
    assert_equal 1, Task.count
  end
end
Run Code Online (Sandbox Code Playgroud)

测试成功:当我销毁d1时,它会破坏d1的所有后代.因此,在破坏之后只剩下根.

但是,在我向Task添加了before_save回调之后,此测试现在失败了.这是我添加到Task的代码:

before_save :update_descendants_if_necessary

def update_descendants_if_necessary
  handle_parent_id_change if self.parent_id_changed?
  return true
end

def …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails acts-as-tree

5
推荐指数
1
解决办法
990
查看次数

Rails3中的Acts-as-tree?

我需要在我的Rails3项目中使用Acts As Tree(ruby 1.9.2).但是,github上有很多分叉,所以我不知道我应该选择哪个,哪个被其他开发者使用?在Railsplugins.org上有http://www.railsplugins.org/plugins/376-acts-as-tree,但最后一次提交是从三月...

acts-as-tree ruby-on-rails-3

5
推荐指数
2
解决办法
6979
查看次数

为什么我的根节点最后将自己作为带有acts_as_tree的parent_id?

我有一个现有的树结构,我想添加一个新的根并将现有的根向下移动一个.我写了一个rake任务,除了一件事情以外还可以正常工作.

新的root以一个parent_id结束,该parent_id匹配它的新id而不是NULL.成功更改现有根以将新根作为父级.

# Rake task
desc "Change categories to use new root"
task :make_new_category_root => :environment do
  Company.all.each do |company|
    current_roots = company.root_categories
    new_root = Category.new(name: "New root")
    new_root.company = company
    new_root.parent = nil
    if new_root.save
      current_roots.each do |current|
        current.parent = new_root
        current.save
      end
    end
  end

# Category class, abbreviated
class Category < ActiveRecord::Base
  include ActsAsTree
  acts_as_tree :order => "name"

  belongs_to :company, touch: true
  validates :name, uniqueness: { scope: :company_id }, :if => :root?      
  scope :roots, where(:parent_id => nil)      
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails acts-as-tree

5
推荐指数
1
解决办法
376
查看次数

actions_as_tree vs祖先gem用于树状菜单

我想在我正在处理的Rails应用程序中实现树菜单(树菜单的简单示例).我不确定是否使用acts_as_tree或Ancestry.似乎使用acts_as_tree实现一个简单的树菜单会更容易,但是,Ancestry更受欢迎并且定期维护.有没有人对此有任何想法?

gem ruby-on-rails acts-as-tree

3
推荐指数
1
解决办法
5097
查看次数