Created
June 20, 2010 04:17
-
-
Save sprsquish/445553 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" » ") | |
rescue | |
'Not available' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'