Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkessler/549339ae5b77dcb0b2876caae831d709 to your computer and use it in GitHub Desktop.
Save mkessler/549339ae5b77dcb0b2876caae831d709 to your computer and use it in GitHub Desktop.
How to make breadcrumbs_on_rails render a Bootstrap 4 compatible breadcrumb navigation
# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap 4's conventions.
#
# You can use it with the :builder option on render_breadcrumbs:
# <%= render_breadcrumbs builder: ::BootstrapBreadcrumbsBuilder %>
#
# Note: You may need to adjust the autoload_paths in your config/application.rb file for rails to load this class:
# config.autoload_paths += %W(#{config.root}/lib)
#
class BootstrapBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder
def render
@context.content_tag(:ul, class: 'breadcrumb') do
@elements.collect do |element|
render_element(element)
end.join.html_safe
end
end
def render_element(element)
current = @context.current_page?(compute_path(element))
@context.content_tag(:li, class: "breadcrumb-item #{'active' if current}") do
@context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment