Skip to content

Instantly share code, notes, and snippets.

@sprsquish
Created June 20, 2010 04:17
Show Gist options
  • Save sprsquish/445553 to your computer and use it in GitHub Desktop.
Save sprsquish/445553 to your computer and use it in GitHub Desktop.
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] =~ /^[0-9]*$/
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
def get_bread_crumb(url)
breadcrumb = []
so_far = []
elements = url.split('/')
last_element = elements.last
elements.each_with_index do |element, index|
so_far << element
url = so_far.join('/')
breadcrumb << if element =~ /^[0-9]*$/
link_to_if(element != last_element, elements[i-1].constantize.find(element).name.humanize, url) rescue element
else
link_to_if(element != last_element, element.titleize, url)
end
end
breadcrumb.join(" &raquo; ")
rescue
'Not available'
end
@jackdesert
Copy link

I forked your code: https://gist.github.com/jackdesert/5117696

I split out some of your longer lines of code,
and fixed a bug where you are using 'i' instead of 'index'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment