URL based on acts_as_tree using routes

Hi There!

I’d like some feedback before I try to implement some funky rails
routes based on an acts_as_tree model.

I have a Page model that acts_as_tree. I’d like the URLs to look
something like this:

http://localhost/about-us/staff/jeff

or, another example…

http://localhost/what-we-do/products/our-great-cms/features/faqs/

So, as many sub-nodes the user creates, that’s how long the URL gets to
be.

Now, it would be great in routes.rb if you just specify a model for
the URL to act as, but the only way I thought of doing this was to
take the path_info manually and splitting the URL by the / into an
array and loop through the array to ensure that all of the nodes along
the way exist.

Here is a route that I am playing with now:

map.connect ‘:controller/:action/:id’
map.connect ‘*path_info’, :controller => ‘pages’, :action => ‘view’

This works kind of okay because if I put the default route first, it
won’t break any existing application functionality I have. Then, in
the “view” action I can now have the params[:path_info] variable do
my bidding.

So, this seems like a good solution but not a great solution. I feel
like the controller shouldn’t have to steal the job of routes.rb.

In Psuedo “i-dont-know-ruby-that-well” code, this would be nice:

map.connect ‘:acts_as_tree’, :controller => ‘pages’, :action => ‘view’,
:acts_as_tree => { :model => Page,
:write_url_for => Page.name,
:root_id => 0,
:default_url => Page.find_by_id(:first) }

OR, with a URL prefix such as “pages”:

map.connect ‘pages/:acts_as_tree’, :controller => ‘pages’, :action =>
‘view’,
:acts_as_tree => { :model => Page,
:write_url_for => Page.name,
:root_id => 0,
:default_url => Page.find_by_id(:first) }

If there is something I am missing, please let me know. I’m hoping
this is an “of-course-rails-does-that” moments.

Thanks!

Jeff

On Friday, June 16, 2006, at 11:17 PM, Jeff W. wrote:

or, another example…

So, this seems like a good solution but not a great solution. I feel
OR, with a URL prefix such as “pages”:

Thanks!

Jeff


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

What happens if a user types in an arbitrary (and probably invalid)
route?
Is the name of each node unique?

A quick and dirty way to do this would be to generate the ‘string-key’
for each record in the tree… say
‘what-we-do/products/our-great-cms/features/faqs/’, and then just look
up your records by this.

Info.find_by_string_key(params[:string_id])

You would have to generate the string key when you create or update the
record. You would need some sort of routine to update the string_ids
for the entire tree to make sure they are consistent.

_Kevin

What I ended up doing was having a URLs table (Url model) that has the
full path to the page that I save when the page is created. When the
page is renamed or moved, there is a function that recurses the tree
and inserts new records into the URLs table. This allows me to save
old paths to old pages so in the event a page changes or moves and
that URL is requested I can redirect the visitor to the location of
the new URL. Oh, and I am using the *path_info as the parameter to
search by… and yes all the nodes are unique.

Again, it just seems like I’m having the controller do the job of
routes and I hope my routes wish I mentioned would come true :slight_smile:

It makes me nervous doing too much with routes right now because I
just saw a Rails-Core thread about a routes re-write:
http://www.ruby-forum.com/topic/67534… so maybe they will come up
with something I can use. I’ve seen other people looking for routes to
use acts_as_tree so I know I’m not the only one.

Jeff

On 17 Jun 2006 12:43:35 -0000, Kevin O.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs