Breadcrumbs in Rails

by on June 19, 2010


I’ve been working a little with Ruby on Rails recently.  One of the things I needed was a good module for handling navigational breadcrumbs.  I googled around a bit, but wasn’t able to find anything that fit my needs.  The closest to what I wanted was this example on stackoverflow.  It was a great starting point, but I ran into issues when I wanted to use nested controllers.  I wanted something a little more flexible.  Here’s my final solution.  I’m still a complete noob at rails, so feel free to point out any improvements you may have.

Features:

  • Splits up the URL and looks up the controllers for each section
  • Assumes you have a ‘name’ field on your data structure
  • Doesn’t link the last item since it should be the current pag
  • Works with nested controllers
  • Converts underscores to spaces and titleize’s the labels
  • Easy to modify to fit your own purposes

Code:

def get_bread_crumb(url)
    begin
        breadcrumb = ''
        so_far = '/'
        elements = url.split('/')
        for i in 1...elements.size
       
            so_far += elements[i] + '/'
           
            if elements[i] =~ /^\d+$/
                begin
                    breadcrumb += link_to_if(i != elements.size - 1, eval("#{elements[i - 1].singularize.camelize}.find(#{elements[i]}).name").gsub("_"," ").to_s, so_far)
                rescue
                    breadcrumb += elements[i]
                end
            else
                breadcrumb += link_to_if(i != elements.size - 1,elements[i].gsub("_"," ").titleize, so_far)
            end
           
            breadcrumb += " » " if i != elements.size - 1
        end
        breadcrumb
    rescue
        'Not available'
    end
end

Usage:

<%= get_bread_crumb(request.request_uri) %>

Which converts a URL like this:

/admin/posts/12/comments/8

to breadcrumbs like this:

Admin » Posts » Breadcrumbs in Rails » Comments » Great Post!

Integrity

by Josh Fraser on June 16, 2010


I remember a couple weeks after I graduated college going to the movies with some friends.  The girl behind the counter asked "student?"  We all said "yes".  Of course, none of us were technically students anymore, but the girl behind the counter ...

Hey Josh, what are you doing now?

by Josh Fraser on June 13, 2010


That's the question I've been getting about 10 times a day lately.  It's been 4 months since we shut down EventVue and my life has changed a lot since then.  I wanted to give a quick personal update to those of you who care about me and are interested. After ...

My talk on AB testing at BDNT

by Josh Fraser on May 9, 2010


On Tuesday I gave a 5 minute talk at the Boulder New Tech Meetup on the value and dangers of AB testing. It's an area where I've made mistakes in the past and it was fun to share my perspective on the topic. Here's the entire video from the event. ...

Simple expandable menu with jQuery

by Josh Fraser on May 1, 2010


I recently needed a nested and expandable navigation system (accordion style) for a project I was working on.  I took a look around at the existing jquery plugins and was surprised by the complexity of them. To me, there's no reason for a simple menu ...

How to find angel investors

by Josh Fraser on February 22, 2010


Recently I've been receiving a lot of emails from entrepreneurs asking for advice. I've learned a lot over the last few years and it's always fun to try and apply the lessons I have learned to another situation. Although I feel unqualified to be ...

Did Google Reader just turn on full PuSH support?

by Josh Fraser on February 18, 2010


Jesse claims the answer is yes. This is a test to find out for myself. Update: It worked!  If your blog is PubSubHubbub enabled any new posts will show up in Google Reader immediately instead of taking the usual 20-30 minutes.  Now you have more ...

My interview on Mixergy

by Josh Fraser on February 15, 2010


If you haven't heard the news yet, Rob and I have decided to shut down EventVue.  As you can probably imagine, it was a incredibly hard decision.  We've poured the last three years of our lives into EventVue and it's tough to say goodbye. One of ...

Updated PubSubHubbub plugin for WordPress MU

by Josh Fraser on January 23, 2010


I just updated my PubSubHubbub Wordpress plugin to work with multi-user installations of Wordpress and also verified that everything still works with version 2.9.1 of Wordpress. If you haven't installed the plugin yet, I encourage you to check it out. ...

My talk from The AJAX Experience

by Josh Fraser on January 19, 2010


Back in September 2008 I had the privilege of speaking at The AJAX Experience in Boston.  I talked about designing great forms and covered a lot of my own pet-peeves with forms on the web and shared some of the things I've learned along the way. Today ...