Forum: Ruby on Rails How do I emulate directory structure with routes?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0ab8e48dbc6cb962e9145ac526522784?d=identicon&s=25 John Athayde (Guest)
on 2006-02-12 17:01
(Received via mailing list)
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.com/
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/

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/
tld.com/projects/:project_name/:sequence_acronym/
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 Athayde
bobo@meticulous.com

Meticulous | www.meticulous.com (work)
Rotoscope | www.rotoscope.com (sound: rock band)
Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic)
Personal Weblog | 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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2006-02-12 18:21
(Received via mailing list)
Hi !

2006/2/12, John Athayde <bobo@meticulous.com>:
> 
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 !
0ab8e48dbc6cb962e9145ac526522784?d=identicon&s=25 John Athayde (Guest)
on 2006-02-13 00:58
(Received via mailing list)
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 Beausoleil wrote:

>> project is the parent of sequence
> map.product_list 'products/*category_path', :controller =>
> 'admin/product_catalog/products', :action => 'public_index'
>
--

John Athayde
bobo@meticulous.com

Meticulous | www.meticulous.com (work)
Rotoscope | www.rotoscope.com (sound: rock band)
Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic)
Personal Weblog | 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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2006-02-13 02:42
(Received via mailing list)
Hi !

2006/2/12, John Athayde <bobo@meticulous.com>:
> 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

will retrieve the latest version of the budget, while

http://localhost/projects/zoomer/finance/budget/4

will retrieve version 4 of the budget.

Ask away if you need more info !

Bye !
0ab8e48dbc6cb962e9145ac526522784?d=identicon&s=25 John Athayde (Guest)
on 2006-02-13 03:04
(Received via mailing list)
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 Beausoleil 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/
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--

John Athayde
bobo@meticulous.com

Meticulous | www.meticulous.com (work)
Rotoscope | www.rotoscope.com (sound: rock band)
Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic)
Personal Weblog | 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
This topic is locked and can not be replied to.