我有这个Rails模型:(为清晰起见,删除了参数)
class Folder < ActiveRecord::Base
belongs_to :parent, :class_name => :folder
has_many :children, :class_name => :folder
end
我希望这个模型像文件系统文件夹一样使用。如何配置路由和控制器以实现此目的?
答案 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