• Stars
    star
    937
  • Rank 46,841 (Top 1.0 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple Ruby on Rails plugin for creating and managing a breadcrumb navigation.

Breadcrumbs On Rails

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.

Build Status Tidelift dependencies

Links

Requirements

  • Ruby >= 2.4
  • Rails >= 5

For older versions use a previous release.

Installation

Add this line to your application's Gemfile:

gem "breadcrumbs_on_rails"

And then execute bundle to install the dependencies:

bundle

Use Bundler and the :git option if you want to grab the latest version from the Git repository.

Usage

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.

class MyController

  add_breadcrumb "home", :root_path
  add_breadcrumb "my", :my_path

  def index
    # ...

    add_breadcrumb "index", index_path
  end

end

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.

class MyController
  def index
    add_breadcrumb "index", index_path, title: "Back to the Index"
  end
end

In your view, you can render the breadcrumb menu with the render_breadcrumbs helper.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>untitled</title>
</head>

<body>
  <%= render_breadcrumbs %>
</body>
</html>

render_breadcrumbs understands a limited set of options. For example, you can pass change the default separator with the :separator option.

<body>
  <%= render_breadcrumbs :separator => ' / ' %>
</body>

Current possible options are:

  • :separator
  • :tag

To use with Bootstrap you might use the following:

<body>
  <ol class="breadcrumb">
    <%= render_breadcrumbs :tag => :li, :separator => "" %>
  </ol>
</body>

More complex customizations require a custom Builder.

HTML Escaping

The text of the breadcrumb is html-escaped by default unless it is a SafeBuffer, to prevent XSS attacks.

class MyController
  add_breadcrumb "This is the <b>Main</b> page".html_safe

  def profile
    add_breadcrumb "#{@user_name} Profile", users_profile
  end
end

In this case, if @user_name is <script>, it will output:

This is the <b>Main</b> page > &lt;script&gt; Profile

Breadcrumb Element

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

Symbol

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.

class MyController
  
  # The Name is set to the value returned by
  # the :root_name method.
  add_breadcrumb :root_name, "/"
  
  protected
  
    def root_name
      "the name"
    end
  
end

Proc

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.

class MyController

  # The Name is set to the value returned by
  # the :root_name method.
  add_breadcrumb Proc.new { |c| c.my_helper_method },
                 "/"

end

String

If the value is a String, the library sets the Element attribute to the string value.

class MyController

  # The Name is set to the value returned by
  # the :root_name method.
  add_breadcrumb "homepage", "/"

end

Restricting breadcrumb scope

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.

class PostsController < ApplicationController
  add_breadcrumb "admin", :admin_path
  add_breadcrumb "posts", :posts_path, only: [:index, :show]
end

class ApplicationController < ActionController::Base
  add_breadcrumb "admin", :admin_path, if: :admin_controller?
  
  def admin_controller?
    self.class.name =~ /^Admin(::|Controller)/
  end
end

Internationalization and Localization

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.

# config/locales/en.yml
en:
  breadcrumbs:
    homepage: Homepage
    first: First
    second: Second
    third: Third

# config/locales/it.yml
it:
  breadcrumbs:
    homepage: Homepage
    first: Primo
    second: Secondo
    third: Terzo

In your controller, use the I18n.t method.

class PostsController < ApplicationController
  add_breadcrumb I18n.t("breadcrumbs.first"),  :first_path
  add_breadcrumb I18n.t("breadcrumbs.second"), :second_path, only: [:second]
  add_breadcrumb I18n.t("breadcrumbs.third"),  :third_path,  only: [:third]
end

class ApplicationController < ActionController::Base
  add_breadcrumb I18n.t("breadcrumbs.homepage"), :root_path
end

Support

Library documentation is auto-generated from the README and the source code, and it's available at https://rubydoc.info/gems/breadcrumbs_on_rails.

Commercial support available as part of the Tidelift Subscription - Learn more

The maintainers of breadcrumbs_on_rails and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Consider subscribing to Tidelift which provides Enterprise support for this project.

Credits

BreadcrumbsOnRails was created and is maintained by Simone Carletti. Many improvements and bugfixes were contributed by the open source community.

Security and Vulnerability Reporting

Full information and description of our security policy please visit SECURITY.md

Changelog

See the CHANGELOG.md file for details.

License

Copyright (c) 2009-2020 Simone Carletti. This is Free Software distributed under the MIT license.

More Repositories

1

whois

An intelligent โ€” pure Ruby โ€” WHOIS client and parser.
Ruby
1,096
star
2

publicsuffix-ruby

Domain name parser for Ruby based on the Public Suffix List.
Ruby
609
star
3

tabs_on_rails

Tabs on Rails is a simple Rails plugin for creating and managing tabs and navigation menus.
Ruby
295
star
4

publicsuffix-go

Domain name parser for Go based on the Public Suffix List.
Go
170
star
5

whois-parser

An intelligent โ€” pure Ruby โ€” WHOIS parser.
Ruby
97
star
6

dnscaa

Go
69
star
7

www-delicious

Ruby client for delicious.com API.
Ruby
62
star
8

actionmailer_with_request

A simple plugin to make the Rails request context available for generating URLs in ActionMailer.
Ruby
54
star
9

apachelogregex

Ruby parser for Apache log files based on regular expressions.
Ruby
54
star
10

ruby-4-rails.showoff

Learning Ruby, with Rails in mind.
Ruby
45
star
11

rubyist

Ruby Quality Guidelines
Ruby
34
star
12

brighella

A simple URL-masking redirect service built on Go.
Go
14
star
13

apachelog2feed

ApacheLogAnalyzer2Feed is a really powerful open source PHP 5 class to parse and analyze Apache Web Server log files.
PHP
13
star
14

activerecord-multiconditions

DISCONTINUED - An ActiveRecord plugin for dealing with complex search :conditions.
Ruby
8
star
15

docbook5-tmbundle

TextMate bundle for DocBook 5.
6
star
16

whois-debian

Various versions of the Whois debian packages tracked in a single Git repository.
C
5
star
17

wordpress-mtimporter

WordPress Importer utilities to finalize your migration from Movable Type to Wordpress.
PHP
5
star
18

helperful

DISCONTINUED - A collection of useful Rails helpers.
Ruby
5
star
19

asg

ASP Stats Generator
ASP
4
star
20

fileiterator

PHP
4
star
21

whois.js

JavaScript
3
star
22

gomotion2015

A practical introduction to go
Go
3
star
23

movabletype-templates

3
star
24

kirbybase

Unofficial Git repository for KirbyBase
3
star
25

letsencrypt-dnsimple

Go
3
star
26

pslint

PSLint is a linter for Public Suffix list
Go
1
star
27

squeezon-ruby

Ruby
1
star
28

digweb

Like DiG, but in HTTP flavour.
Go
1
star
29

hubot-digweb

CoffeeScript
1
star
30

domainr-go

A Go client for the Domainr API.
Go
1
star
31

public_suffix_service

The repository has moved!
1
star