开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜