Name: breadcrumbs_on_rails
Owner: Stembolt
Description: A simple Ruby on Rails plugin for creating and managing a breadcrumb navigation.
Forked from: CasperSleep/breadcrumbs_on_rails
Created: 2017-03-28 22:22:36.0
Updated: 2017-03-28 22:22:38.0
Pushed: 2017-03-28 22:23:58.0
Homepage: http://www.simonecarletti.com/code/breadcrumbs_on_rails
Size: 115
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
BreadcrumbsOnRails is a simple Ruby on Rails plugin for creating and managing a breadcrumb navigation for a Rails project. It provides helpers for creating navigation elements with a flexible interface.
Please note
RubyGems is the preferred way to install BreadcrumbsOnRails and the best way if you want install a stable version.
$ gem install breadcrumbs_on_rails
Specify the Gem dependency in the Bundler Gemfile
.
gem "breadcrumbs_on_rails"
Use Bundler and the :git
option if you want to grab the latest version from the Git repository.
Creating a breadcrumb navigation menu in your Rails app using BreadcrumbsOnRails is really straightforward.
In your controller, call add_breadcrumb
to push a new element on the breadcrumb stack. add_breadcrumb
requires two arguments: the name of the breadcrumb and the target path.
s MyController
d_breadcrumb "home", :root_path
d_breadcrumb "my", :my_path
f index
# ...
add_breadcrumb "index", index_path
d
See the section “Breadcrumb Element” for more details about name and target class types.
The third, optional argument is a Hash of options to customize the breadcrumb link.
s MyController
f index
add_breadcrumb "index", index_path, :title => "Back to the Index"
d
In your view, you can render the breadcrumb menu with the render_breadcrumbs
helper.
CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
l xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
d>
eta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
itle>untitled</title>
ad>
y>
= render_breadcrumbs %>
dy>
ml>
render_breadcrumbs
understands a limited set of options. For example, you can pass change the default separator with the :separator
option.
y>
= render_breadcrumbs :separator => ' / ' %>
dy>
Current possible options are:
:separator
:tag
To use with Bootstrap you might use the following:
y>
l class="breadcrumb">
<%= render_breadcrumbs :tag => :li, :separator => "" %>
ol>
dy>
More complex customizations require a custom @Builder@.
A breadcrumbs menu is composed by a number of Element
objects. Each object contains two attributes: the name of the breadcrumb and the target path.
When you call add_breadcrumb
, the method automatically creates a new Element
object for you and append it to the breadcrumbs stack. Element
name and path can be one of the following Ruby types:
Symbol
Proc
String
If the value is a Symbol
, the library calls the corresponding method defined in the same context the and sets the Element
attribute to the returned value.
s MyController
The Name is set to the value returned by
the :root_name method.
d_breadcrumb :root_name, "/"
otected
def root_name
"the name"
end
If the value is a Proc
, the library calls the proc passing the current view context as argument and sets the Element
attribute to the returned value. This is useful if you want to postpone the execution to get access to some special methods/variables created in your controller action.
s MyController
The Name is set to the value returned by
the :root_name method.
d_breadcrumb Proc.new { |c| c.my_helper_method },
"/"
If the value is a String
, the library sets the Element
attribute to the string value.
s MyController
The Name is set to the value returned by
the :root_name method.
d_breadcrumb "homepage", "/"
The add_breadcrumb
method understands all options you are used to pass to a Rails controller filter. In fact, behind the scenes this method uses a before_filter
to store the tab in the @breadcrumbs
variable.
Taking advantage of Rails filter options, you can restrict a tab to a selected group of actions in the same controller.
s PostsController < ApplicationController
d_breadcrumb "admin", :admin_path
d_breadcrumb "posts", :posts_path, :only => %w(index show)
s ApplicationController < ActionController::Base
d_breadcrumb "admin", :admin_path, :if => :admin_controller?
f admin_controller?
self.class.name =~ /^Admin(::|Controller)/
d
BreadcrumbsOnRails is compatible with the standard Rails internationalization framework.
For example, if you want to localize your menu, define a new breadcrumbs node in your .yml
file with all the keys for your elements.
nfig/locales/en.yml
eadcrumbs:
homepage: Homepage
first: First
second: Second
third: Third
nfig/locales/it.yml
eadcrumbs:
homepage: Homepage
first: Primo
second: Secondo
third: Terzo
In your controller, use the I18n.t
method.
s PostsController < ApplicationController
d_breadcrumb I18n.t("breadcrumbs.first"), :first_path
d_breadcrumb I18n.t("breadcrumbs.second"), :second_path, :only => %w(second)
d_breadcrumb I18n.t("breadcrumbs.third"), :third_path, :only => %w(third)
s ApplicationController < ActionController::Base
d_breadcrumb I18n.t("breadcrumbs.homepage"), :root_path
BreadcrumbsOnRails was created and is maintained by Simone Carletti. Many improvements and bugfixes were contributed by the open source community.
Direct questions and discussions to Stack Overflow.
Pull requests are very welcome! Please include tests for every patch, and create a topic branch for every separate change you make.
Report issues or feature requests to GitHub Issues.
Copyright (c) 2009-2015 Simone Carletti. This is Free Software distributed under the MIT license.