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