DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Nested Pathing For Basic Routes

08.17.2010
| 5452 views |
  • submit to reddit
        If you are using simple resources and actions you can use the following snippet to make simple path links.
such as Home > Items > New
First add this to the application helper.
module ApplicationHelper

  def path_links
    link_to("Home", controller.parent_path) 
      + " > " + link_to(controller.controller_name, :controller => controller.controller_path, :action => :index) 
      + " > " + link_to(controller.action_name, :controller => controller.controller_path, :action => controller.action_name)
  end

end

Now in your layout simply call path_links. You will get something like the following
<a href="/system">Home</a> > <a href="/system/customers">customers</a> > <a href="/system/customers/new">new</a>

Now add the new method to the application_controller
def parent_path
  "/"
end

If you desire to use namespaces like /system/users/new then simply override the parent_path method in the base controller for that namespace.
class SystemController < ApplicationController
  def parent_path
    "/system"
  end
end

I'm working on a more robust method to capture all of the possible routes.