Nested URL Routing

Hi everyone,

I've been reading a lot of posts related to routing and URLS in Rails
lately but I still can't figure out how to do this:

I have a controller (categories) where I'm using the BetterNestedSet
plugin in order to have a tree structure for categories for my web
project. A "category" is basically like a folder.

I also have another controller (pages) where each "page" is
essentially an HTML-like page. Each "page" is filed underneath a
certain "category". All of this seems to work at the moment. But I
can't seem to find any information on how to display a "page" as if it
were filed (like an HTML file in a web directory.) So that it would
look something like this:

http://www.site.com/pages/category_a/category_b/category_c/page

Maybe this is the point where you (the reader,) are wondering, "why is
he doing it THIS way...?" And if so, is there a better way to do
this? I realize that if I eventually need to move a page to another
"category" the URL where it was at previously will now be broken. But
for a simple website application it works for me.

Is there a way to have a route that allows for potentially unlimited
directories in the URL?

thanks,

andy

Yep, you can use route globbing:

map.file ‘*directory/:file’, :requirements => {:file => /\w+.\w+/}

map.directory ‘*directory’

Then to generate lins to these URLs:

directory_path([‘path’, ‘to’, ‘dir’])
file_path([‘path’, ‘to’, ‘dir’], ‘file.txt’)

The trick with globbing is that you need to have parts in your URL that either conform to some requirements, or have a unique piece in them. Otherwise, any route will match that route.

Hope that helps.

Brandon