2009-10-17 108 views
0

我有這個Rails的模型:(爲清楚起見移除參數)我該如何處理類似文件夾的模型結構?

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

我要像使用文件系統文件夾這種模式。我該如何配置路線和控制器才能實現這一點?

回答

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 
相關問題