Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs.
In your Gemfile:
gem 'gretel', '2.0.0.beta1'
And run:
$ bundle install
Start by generating an initializer:
$ rails generate gretel:install
Then, in config/initializers/breadcrumbs.rb:
Gretel::Crumbs.layout do
# Root crumb
crumb :root do
link "Home", root_path
end
# Regular crumb
crumb :projects do
link "Projects", projects_path
end
# Parent crumbs
crumb :project_issues do |project|
link "Issues", project_issues_path(project)
parent :project, project
end
# Child
crumb :issue do |issue|
link issue.name, issue_path(issue)
parent :project_issues, issue.project
end
# Multiple links per crumb (recursive links for parent categories)
crumb :category do |category|
parents = [category]
parent_category = category
while parent_category = parent_category.parent_category
parents.unshift parent_category
end
parents.each do |category|
link category.name, category
end
parent :categories
end
# Product crumb with recursive parent categories
crumb :product do |product|
link product.name, product
parent :category, product.category
end
# Multiple arguments
crumb :multiple_test do |a, b, c|
link "Test #{a}, #{b}, #{c}", test_path
parent :other_test, 3, 4, 5
end
end
At the top of app/views/issues/show.html.erb, set the current breadcrumb:
<% breadcrumb :issue, @issue %>
Then, in app/views/layouts/application.html.erb:
<%= breadcrumbs :pretext => "You are here: ",
:separator => " › ",
:semantic => true %>
This will generate a <div class="breadcrumbs">
containing the breadcrumbs.
If you supply a block to the breadcrumbs
method, it will yield an array with the breadcrumb links so you can build the breadcrumbs HTML manually:
<% breadcrumbs do |links| %>
<% if links.any? %>
You are here:
<% links.each do |link| %>
<%= link_to link.text, link.url %> (<%= link.key %>)
<% end %>
<% end %>
<% end %>
You can pass options to <%= breadcrumbs %>
, e.g. <%= breadcrumbs :pretext => "You are here:" %>
:
Option | Description | Default |
---|---|---|
:pretext | Text to be rendered before breadcrumb, e.g. "You are here: " |
None |
:posttext | Text to be appended after breadcrumb, e.g. "Text after breacrumb" |
None |
:separator | Separator between links, e.g. " › " |
" > " |
:autoroot | Whether it should automatically link to the :root crumb if no parent is given. |
False |
:show_root_alone | Whether it should show :root if that is the only link. |
False |
:link_current | Whether the current crumb should be linked to. | False |
:semantic | Whether it should generate semantic breadcrumbs. | False |
:id | ID for the breadcrumbs container. | None |
:class | CSS class for the breadcrumbs container. | "breadcrumbs" |
:current_class | CSS class for the current link or span. | "current" |
Copyright (c) 2010-2013 Lasse Bunk, released under the MIT license