Breadcrumbs
Breadcrumbs is a simple plugin that adds a breadcrumbs
object to controllers
and views.
Instalation
Just run gem install breadcrumbs
. Or add gem "breadcrumbs"
to your Gemfile.
Usage
On your controller (optional):
class ApplicationController < ActionController::Base
before_filter :add_initial_breadcrumbs
private
def
.add "Home", root_path
end
end
class ThingsController < ApplicationController
def index
.add "Things", things_path
end
end
You don't need to provide an URL; in that case, a span will be generated instead of a link:
.add "Some page"
You can set additional HTML attributes if you need to:
.add "Home", root_path, id: "home", title: "Go to the home page"
On your view (possibly application.html.erb):
<%= breadcrumbs.render %>
You can render as ordered list.
<%= breadcrumbs.render(format: :ordered_list) %>
You can render as inline links.
<%= breadcrumbs.render(format: :inline) %>
You can set your own separator:
<p>
You are here: <%= breadcrumbs.render(format: :inline, separator: "|") %>
</p>
You can also define your own formatter. Just create a class that implements a
render
instance method and you're good to go.
class Breadcrumbs::Render::Dl
def render
# return breadcrumbs wrapped in a <dl> tag
end
end
To use your new format, just provide the :format
option.
.render(format: :dl)
I18n
Breadcrumbs is integrated with I18n. You can set translations like:
en:
breadcrumbs:
home: "Home"
And then you just call
.add :home
In fact, you can provide any scope you want.
.add :"titles.home"
If you don't want to translate a label, just pass the option :i18n
as false
.
.add :home, nil, i18n: false
Maintainer
- Nando Vieira - http://nandovieira.com
License
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.