Creating nested routes for pages that use acts_as_tree
I have a Page model that goes 1 generation deep. How can I do my routes so that
/about
/about/leadership
/about/vision-and-mission
/contact
/join
/join/benefi开发者_运维技巧ts
work?
The slugs come from the friendly_id plugin, and are all unique.
At the end of your routes:
map.with_options :controller => 'pages' do |pages|
pages.show_page ':id', :action => 'show'
pages.show_page_with_parent ':id/:parent_id', :action => 'show'
end
If you wand to generate a link to it:
show_page_with_parent_path(:id => page.name, :parent_id => page.parent.name)
This, of course, assuming that the slug key is the name.
You may, of course, further customise this solution, with a helper function:
EG:
def custom_show_page_path(page)
if page.parent.nil?
show_page_path(:id => page.name)
else
show_page_with_parent_path(:id => page.name, :parent_id => page.parent.name)
end
end
精彩评论