如何处理类似文件夹的模型结构?

时间:2009-10-17 15:58:33

标签: ruby-on-rails

我有这个Rails模型:(为清晰起见,删除了参数)

class Folder < ActiveRecord::Base
  belongs_to :parent, :class_name => :folder 
  has_many :children, :class_name => :folder
end

我希望这个模型像文件系统文件夹一样使用。如何配置路由和控制器以实现此目的?

1 个答案:

答案 0 :(得分:1)

1)至于模型:查看acts_as_tree

2)至于路线:做类似

的事情
map.folder '/folders/*path', :controller => 'folders', :action => 'show'

并在FoldersController

def show
  # params[:path] contains an array of folder names
  @folder = Folder.root
  params[:path].each |name|
    @folder = @folder.children.find_by_name(name) or raise ActiveRecord::RecordNotFound
  end
  # there you go, @folder contains the folder identified by the path
end
相关问题