Reputation: 72504
I have this Rails model: (Parameters removed for clarity)
class Folder < ActiveRecord::Base
belongs_to :parent, :class_name => :folder
has_many :children, :class_name => :folder
end
I want this model to be used like a file system folder. How do I have to configure the routes and the controller to make this possible?
Upvotes: 0
Views: 94
Reputation: 14179
1) As for the model: check out acts_as_tree
2) As for the routes: do something like
map.folder '/folders/*path', :controller => 'folders', :action => 'show'
and in the 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
Upvotes: 1