Skip to content

Instantly share code, notes, and snippets.

@emartini
Created February 16, 2012 13:48
Show Gist options
  • Save emartini/1844935 to your computer and use it in GitHub Desktop.
Save emartini/1844935 to your computer and use it in GitHub Desktop.
Really simple breadcrumbs for Rails
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include Breadcrumbs
# The rest of your application controller...
# ...
end
#lib/breadcrumbs.rb
module Breadcrumbs
class Element
attr_accessor :name, :path
def initialize(name, path, options={})
@name = name
@path = path
end
end
def add_breadcrumb(name, path)
self.breadcrumbs << Breadcrumbs::Element.new(name, path)
end
def breadcrumbs
@breadcrumbs ||= []
end
end
#app/helpers/breadcrumbs_helper.rb
module BreadcrumbsHelper
#
# Prints the breadcrumbs for the actual controller's action.
#
# Example:
# #if @breadcrumbs is setted:
# breadcrumbs_display
# # => "<ul class='breadcumbs'><li><a href="some_url">Breadcrumb</li></ul>"
# #else
# breadcrumbs_display
# # => ""
#
# Returns an HTML unordered list if application breadcrumbs isn't blank,
# otherwise, an empty string
#
def breadcrumbs_display
return "" if (@breadcrumbs.blank?)
html_list = "<ul class='breadcumbs'>"
@breadcrumbs.each do |b|
# If no path is supplied, don't add anchor.
if b.path.blank?
html_list << "<li>#{b.name}</li>"
else
html_list << "<li><a href='#{b.path}'>#{b.name}</a></li>"
end
end
html_list << "</ul>"
html_list.html_safe
end
end
ul.breadcumbs{
padding: 4px 0;
margin: 5px 0 10px 0;
list-style: none;
border-bottom: 1px solid #ccc;
li{
color: #333;
display: inline;
margin-right:5px;
&:after{
content: "/";
display: inline-block;
margin-left:5px;
}
&:last-child{
}
&:last-child:after{
content: "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment