Nested Routes

21 views
Skip to first unread message

Mengu

unread,
Dec 25, 2011, 12:02:43 PM12/25/11
to TurboGears
hi everyone,

i have a conceptual question on routes. in rails we would have routes
and nested routes. for example a document has many pages and i could
set up routes like "/documents/:id/pages", "/documents/:id/pages/[new|
create|edit|update|destroy]. is this possible with turbogears?

Michael Pedersen

unread,
Dec 26, 2011, 10:51:20 AM12/26/11
to turbo...@googlegroups.com
Well, the preferred mechanism in TG is to use Object Dispatch. So, it functions differently. Despite it working differently, yes, it is possible.

Your controller has two methods to assist with this:

_default(): This will be called whenever the index method would be called, but there is no index method.
_lookup(): This will be called when your controller is responsible for handling the URL, but no match could be found.

So, for examples (we assume a root controller that *only* has _default and _lookup defined, for sake of simplified examples):

method: _default

method: _lookup

The pattern can be nested deeply, as well, allowing you to have a controller mounted at documents, which would then _lookup the idnum and act appropriately. It could even dispatch the control down to a further nested subcontroller for more localized processing.

--
Michael J. Pedersen
My Online Resume: http://www.icelus.org/ -- Google+ http://plus.ly/pedersen
Google Talk: m.ped...@icelus.org -- Twitter: pedersentg

Mengu

unread,
Dec 26, 2011, 8:18:40 PM12/26/11
to TurboGears
hi michael,

thank you very much for your answer. after reading your post, i did
dive into the source files under tg/controllers and the documents.
your post and the http://www.turbogears.org/2.1/docs/main/TGControllers.html
page was very helpful.

for future references if anyone needs, here is how i am using nested
routes with TG 2.1.4 -as it's the current version right now-:

this is an exempt of my DocumentsController class:
http://pastie.org/private/1vdjcqo2lbdj6p3zja7a

and this is my PageController class: http://pastie.org/private/wleeeoph90geuly2bdh1g

thanks again.

On 26 Aralık, 17:51, Michael Pedersen <m.peder...@icelus.org> wrote:
> Google Talk: m.peder...@icelus.org -- Twitter: pedersentg

Moritz Schlarb

unread,
Mar 17, 2012, 7:51:45 AM3/17/12
to turbo...@googlegroups.com
Hey Mengu, thanks for your examples,

I just ran into the same problem as you and your solutions really helped me out!

Best wishes,

Moritz

PS: Michael, I think it would be very nice to have this example somewhere in the TG docs, wouldn't it?

Alessandro Molina

unread,
Mar 17, 2012, 7:58:38 AM3/17/12
to turbo...@googlegroups.com
The 20 Minutes Wiki tutorial is a good example about using _default
(http://www.turbogears.org/2.1/docs/main/Wiki20/wiki20.html) but we
indeed miss an example for _lookup.

> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/XMdj5m7UUo0J.
>
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.

Reply all
Reply to author
Forward
0 new messages