Simple Breadcrumb in Ruby on Rails
I thought I might find a short snippet, plugin, gem, whatever, somewhere on the web that would translate my request URI into a simple breadcrumb. Unfortunately, I didn't find anything within the first several pages of the several different Google searches I performed, so I thought to myself, "How hard could it be?". Not hard at all actually. (Still harder than copying someone else's code though.. hehehehe.)
So, here it goes. My requirements were:
- Breadcrumb nodes should be listed in an unordered list
- The first node in the trail should always be the "home" link.
- The last node in the series should not be clickable - since we're already on that page.
- In the case of an edit, show, or any other action where the id param was stuck onto the end of the URL, I didn't want that id as part of my breadcrumb trail, thus making the action name the last node in the list - and unclickable per #3 above.
- Any other query string params (e.g. http://url/?variable=value) obviously should not wreak havoc on my breadcrumbs either.
- Translate all underscores "_" into spaces.
Now, I realize its pretty crude at this point. It appears to be working for what I need it for. Hopefully someone else finds this and can make it better in some way.
<ul>
<li><a href="/">home</a></li>
<%=
s = ""
url = request.path.split('?') #remove extra query string parameters
levels = url[0].split('/') #break up url into different levels
levels.each_with_index do |level, index|
unless level.blank?
if index == levels.size-1 ||
(level == levels[levels.size-2] && levels[levels.size-1].to_i > 0)
s += "<li>#{level.gsub(/_/, ' ')}</li>\n" unless level.to_i > 0
else
link = "/"
i = 1
while i <= index
link += "#{levels[i]}/"
i+=1
end
s += "<li><a href=\"#{link}\">#{level.gsub(/_/, ' ')}</a></li>\n"
end
end
end
s
-%>
</ul>
This was stuck in a partial and rendered from within my application layout. Not sure how this would handle performance wise, but right now its doing the job!
Its still a bit perplexing that nothing of this sort has already been written elsewhere. There's probably a more elegant solution than the one I've came up with.




Comments
Great script
I also was looking around for something like this. This script was perfect and after a few minutes fiddling and styling with CSS was a great little solution!
Thanks very much Josh!
James
Why not?
>> "Its still a bit perplexing that nothing of this sort has already been written elsewhere. "
i think that are two main reasons because not generic solutions are available.
1. Has no good way to do this *GENERIC*! Breadcrumbs reflect specific aspects about your application and in few cases is more than just split controller / action in li items. Many questions should impact on what i want with my breadcrumbs... I want show where the guy is, or how he come here? I want items as links or not? and if want links, for where should they point to?... I always want just the name of the action (with gsub('_', ' ')) or i want customized titles for my itens? This is just a small set of questions. The biggest point here is: this is not very very application specific feature?
2. for simple solutions as you presented, less than 20 lines of pure ruby code is needed! You really think that has reasons for a plugin???
I suggested that I might
I suggested that I might come across a code snippet, plugin, or some other source that was already doing this. I wasn't advocating that a plugin be created just for this reason as I do agree, its simple code.
Also, if you look at when this was posted, its over a year old. A lot has changed in Rails since then, as has my understanding of it.
What if you need specific title for each breacrumb ??
What if you need specific title for each breadcrumb and you just do not want to replace "_" with " " ??
Also, it's not necessary that you would like to display everything from the beginning till end in the url as breadcrumb..
Post new comment