URL-based Breadcrumbs

February 23rd, 2007

Here’s a quick Snip to output URL-based breadcrumbs. Just drop this in your ApplicationHelper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def breadcrumbs
  r = []
  r << link_to("Home", "/")
  url = request.path.split('?')
  segments = url[0].split('/')
  segments.shift
  segments.each_with_index do |segment, i|
    title = segment.gsub(/-/, ' ').titleize
    r << link_to_unless_current(title, "/" + 
         (0..(i)).collect{|seg| segments[seg]}.join("/"))
  end
  return content_tag("div", r.join(" &#187; "), 
         :class => "breadcrumbs")
end

In your view, put:


<%= breadcrumbs %>

If you’re on the page /articles/my-fancy-schmancy-article

You’ll get a breadcrumb like this:

Home » Forum » My Fancy Schmancy Article

Inspiration from joshhuckabee.com/node/58

3418 Responses to “URL-based Breadcrumbs”

    Liquid error: Mysql::Error: MySQL client ran out of memory: SELECT * FROM contents WHERE (contents.article_id = 8 AND (contents.approved = 1)) AND ( (contents.`type` = 'Comment' ) ) ORDER BY created_at

Leave a Reply