# How do I emulate directory structure with routes?

**URL:** <https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632>\
**Category:** Rails\
**Created:** [February 12, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632 "2006-02-12T17:01:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [February 12, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632/1 "2006-02-12T17:01:09Z")

</div>

I’ve got something that I’m not sure is actually doable. I have a  
very complex model relationship with a lot of parent/child/grandchild/  
greatgrandchild etc stuff going on. Is there a way to do a route like  
this?

> **[TLD Computers – Delivering strategic IT solutions and services to businesses...](https://www.tld.com/)**
>
> At TLD computers, we offer a comprehensive range of IT solutions and services, focusing on helping businesses reach their long-term objectives through creative solutions and outstanding customer service.

projects/:project\_name/:sequence\_acronym/:shot\_number/:department/:eleme  
nt\_name/:version/

where:

project is the parent of sequence  
sequence is the parent of shot  
shot and department are parents to element  
element (will) exists in an acts\_as\_versioned model.

or to also show it

[tld.com/projects/:project\_name/:department/:element\_name/:version/](http://tld.com/projects/:project_name/:department/:element_name/:version/)

Which would show a specific version of an element.

I’m also interested in stepping this down as emulated directory  
structure.

[tld.com/projects/:project\_name/](http://tld.com/projects/:project_name/)  
[tld.com/projects/:project\_name/:sequence\_acronym/](http://tld.com/projects/:project_name/:sequence_acronym/)  
[tld.com/projects/:project\_name/:sequence\_acronym/:shot\_number/](http://tld.com/projects/:project_name/:sequence_acronym/:shot_number/)

etc.

Is this kind of stuff doable? Is this acts\_as\_tree (which i’ve seen  
in the book but never in a live implementation)

–

John A.  
removed\_email\_address@domain.invalid

Meticulous | [www.meticulous.com](http://www.meticulous.com) (work)  
Rotoscope | [www.rotoscope.com](http://www.rotoscope.com) (sound: rock band)  
Boboroshi & Kynz | [www.boboroshiandkynz.com](http://www.boboroshiandkynz.com) (sound: electronic)  
Personal Weblog | [www.boboroshi.com](http://www.boboroshi.com) (play)

“Those who would give up essential Liberty, to purchase a little  
temporary Safety, deserve neither Liberty nor Safety.”  
- Benjamin Franklin (1706-1790)  
Reply of the Pennsylvania Assembly to the  
Governor  
November 11, 1755

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [February 12, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632/2 "2006-02-12T18:21:48Z")

</div>

Hi !

2006/2/12, John A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> [tld.com/projects/:project\_name/:sequence\_acronym/:shot\_number/:department/:element\_name/:version/](http://tld.com/projects/:project_name/:sequence_acronym/:shot_number/:department/:element_name/:version/)
> 
> where:
> 
> project is the parent of sequence  
> sequence is the parent of shot  
> shot and department are parents to element  
> element (will) exists in an acts\_as\_versioned model.

No problem. Simply implement the right action, and you’ll be fine. I  
do something similar in a product catalog application. My routes are:

map.product\_details ‘products/details/:product\_no’, :controller =\>  
‘admin/product\_catalog/products’, :action =\> ‘public\_view’,  
:product\_no =\> /^[-\w."'%]+$/  
map.product\_list ‘products/\*category\_path’, :controller =\>  
‘admin/product\_catalog/products’, :action =\> ‘public\_index’

> Is this kind of stuff doable? Is this acts\_as\_tree (which i’ve seen in the  
> book but never in a live implementation)

acts\_as\_tree makes an ActiveRecord model behave as if it were a tree.  
It has nothing to do with routes.

If you need more help, don’t hesitate to ask !

Bye !

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [February 13, 2006, 12:58am UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632/3 "2006-02-13T00:58:18Z")

</div>

I don’t understand how that directly relates to setting up a route  
where almost every value is a variable…

so projects/whitetiger/ would pull the projects/show/2  
projects/whitetiger/opn/ would pull the opening sequence in white  
tiger (sequence is a model where acronym is unique within scope of  
project\_id or the parent)

and so on and so forth.

On Feb 12, 2006, at 12:21 PM, Francois B. wrote:

> > project is the parent of sequence  
> > map.product\_list ‘products/\*category\_path’, :controller =\>  
> > ‘admin/product\_catalog/products’, :action =\> ‘public\_index’

–

John A.  
removed\_email\_address@domain.invalid

Meticulous | [www.meticulous.com](http://www.meticulous.com) (work)  
Rotoscope | [www.rotoscope.com](http://www.rotoscope.com) (sound: rock band)  
Boboroshi & Kynz | [www.boboroshiandkynz.com](http://www.boboroshiandkynz.com) (sound: electronic)  
Personal Weblog | [www.boboroshi.com](http://www.boboroshi.com) (play)

“Those who would give up essential Liberty, to purchase a little  
temporary Safety, deserve neither Liberty nor Safety.”  
- Benjamin Franklin (1706-1790)  
Reply of the Pennsylvania Assembly to the  
Governor  
November 11, 1755

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [February 13, 2006, 3:04am UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632/4 "2006-02-13T03:04:16Z")

</div>

AH!!! Okay. I wasn’t quite understanding the controller aspects of  
how to make it work. Thank you 🙂 Off to play…

On Feb 12, 2006, at 8:42 PM, Francois B. wrote:

> :controller =\> ‘elements’, :action =\> ‘view’, :version =\> nil,  
> @element = @dept.elements.find\_by\_name(params[:element\_name]  
> So,
> 
> ## Bye !
> 
> FranÃ§ois Beausoleil  
> [http://blog.teksol.info/](http://blog.teksol.info/)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

John A.  
removed\_email\_address@domain.invalid

Meticulous | [www.meticulous.com](http://www.meticulous.com) (work)  
Rotoscope | [www.rotoscope.com](http://www.rotoscope.com) (sound: rock band)  
Boboroshi & Kynz | [www.boboroshiandkynz.com](http://www.boboroshiandkynz.com) (sound: electronic)  
Personal Weblog | [www.boboroshi.com](http://www.boboroshi.com) (play)

“Those who would give up essential Liberty, to purchase a little  
temporary Safety, deserve neither Liberty nor Safety.”  
- Benjamin Franklin (1706-1790)  
Reply of the Pennsylvania Assembly to the  
Governor  
November 11, 1755

---

<div class="post-metadata">

**Author:** ![john-anes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@john-anes](https://www.ruby-forum.com/u/john-anes)\
**Post date:** [February 13, 2006, 2:42am UTC](https://www.ruby-forum.com/t/how-do-i-emulate-directory-structure-with-routes/53632/5 "2006-02-13T02:42:47Z")

</div>

Hi !

2006/2/12, John A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I don’t understand how that directly relates to setting up a route where  
> almost every value is a variable…

Let’s take one of your routes as an example:  
map.connect ‘projects/:project\_name/:department/:element\_name/:version’,  
:controller =\> ‘elements’, :action =\> ‘view’, :version =\> nil,  
:requirements =\> {:version =\> /^\d+$/}

This tells Rails to match any URLs from the form above, and call #view  
in ElementsController. Here’s a possible implementation of said  
method:

class ElementsController \< ApplicationController  
def view  
@project = Project.find\_by\_project\_name(params[:project\_name]  
@dept = @project.departments.find\_by\_name(params[:department]  
@element = @dept.elements.find\_by\_name(params[:element\_name]  
if params[:version] then  
@element = @element.find\_version(params[:version])  
end  
end  
end

Notice in the route I put :version =\> nil, :requirements =\> {:version  
=\> /^\d+$/} ? That tells Rails tht the version parameter is optional,  
but if it is present, it must match the given regular expression.

So,

[http://localhost/projects/zoomer/finance/budget](http://localhost/projects/zoomer/finance/budget)

will retrieve the latest version of the budget, while

[http://localhost/projects/zoomer/finance/budget/4](http://localhost/projects/zoomer/finance/budget/4)

will retrieve version 4 of the budget.

Ask away if you need more info !

Bye !
