Skip to content

Instantly share code, notes, and snippets.

@visoft
Created July 7, 2011 23:02
Show Gist options
  • Save visoft/1070758 to your computer and use it in GitHub Desktop.
Save visoft/1070758 to your computer and use it in GitHub Desktop.
Custom Breadcrumb Builder that handles a render action from the controller using breadcrumbs_on_rails. Instead of highlighting the "middle" item as active, e.g. Home >> **Widgets** >> New, the last item will be highlighted instead (so New in the previous
// Usage
= render_breadcrumbs :builder => BreadcrumbBuilder
class BreadcrumbBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder
def render
@elements.collect do |element|
render_element(element)
end.join(@options[:separator] || " &raquo; ")
end
def render_element(element)
# Force the last breadcrumb to be the active one if the request is a post or a put
if @context.request.post? || @context.request.put?
content = (@elements.last == element) ? element.name : @context.link_to(compute_name(element), compute_path(element))
else
content = @context.link_to_unless_current(compute_name(element), compute_path(element))
end
if @options[:tag]
@context.content_tag(@options[:tag], content)
else
content
end
end
end
# The controller code
def new
add_breadcrumb 'New', new_widget_path
end
def create
@widget = Widget.new(params[:widget])
if @widget.save
redirect_to @widget, notice: 'Widget was successfully created.'
else
add_breadcrumb 'New', new_widget_path
render 'new'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment