breadcrumbs_for

Breadcrumbs Rails helper.

Breadcrumbs helper for Rails done in a rails way.

Built on url_for, your routes, model names and i18n.

Installation

gem install breadcrumbs_for

Requirements

Was tested with Rails 3

Usage

Let say:

  • You have a Blog and Post models in your app.

  • Posts are nested in Blogs.

  • You have :admin namespace to manage them.

  • Blog and Post have a ‘title’ or ‘name’ methods

Examples:

breadcrumbs_for :blogs                         # Home / Blogs
breadcrumbs_for :blogs, 'new'                  # Home / Blogs / New
breadcrumbs_for :blogs, @blog                  # Home / Blogs / My blog
breadcrumbs_for :blogs, @blog, 'edit'          # Home / Blogs / My blog / Edit
breadcrumbs_for @blog                          # Home / My blog
breadcrumbs_for :blogs, @blog, [@blog, @post]  # Home / Blogs / My blog / My Post

breadcrumbs_for [:admin,:blogs], [:admin, @blog]
breadcrumbs_for [:admin,:blogs], ['edit', :admin, @blog, @post]
breadcrumbs_for [:admin,:blogs], [:admin, @blog], ['edit', :admin, @blog, @post]

etc.

Note: There is a shorter alias crumbs_for, so you can do:

crumbs_for :blogs, @blog

You can even do the following:

breadcrumbs_for {:controller=>'blogs', :action=>'index', :crumb=>'All Blogs'}, @blog

Note on :crumb=>‘All Blogs’. Here you need to provide the breadcrumb caption text directly via the :crumb key.

If you have an admin_path in your routes you can do:

breadcrumbs_for :admin, [:admin,:blogs], ['new', :admin, @blog], :crumbs_options=>{:root=>false}

Produced HTML

By default it will render breadcrumbs as an unordered list.

The following code

<%= crumbs_for :posts, 'New' %>

will produce html:

<ul class="breadcrumbs">
 <li class="crumb root">
   <a href="/" class="crumb home_crumb">Home</a>
 </li>
 <li class="sep">/</li>
 <li class="crumb ">
   <a href="/posts" class="crumb">Posts</a>
 </li>
 <li class="sep">/</li>
 <li class="crumb active">
   New
 </li>
</ul>

You can also render bredcrumbs in plain.

The following code

<%= crumbs_for :posts, 'New', :crumbs_options => { :type => :plain } %>

will produce html:

Home
/
Themes
/
New

Customize

  • Don’t inculde the Home(root) link:

breadcrumbs_for :blogs, @blog, :crumbs_options=>{:root=>false}
  • By default breadcrumbs are rendered as a ul(html unorderd list). You can disable this:

breadcrumbs_for :blogs, @blog, :crumbs_options=>{:type=>:plain}
  • The default separator is /. Chage it:

breadcrumbs_for :blogs, @blog, :crumbs_options=>{:sep=>'&rarr;'}

Custom captions and i18n

Define captions for breadcrumbs in your locale file:

en:
 breadcrumbs:
   root: 'Home'
   actions:
     new: 'Add new'
     edit: 'Edit one'
   names:
     admin: 'Manage'
     blogs: 'The blogs'
     posts: 'The posts'

Tips & Tricks

When providing action as a String it will include action name in breadcrumb caption.

crumbs_for :blogs, ['edit', @blog]  # => Home / Blogs / Edit My blog

When providing action as a Symbol it will skip action name in breadcrumb caption.

crumbs_for :blogs, [:edit, @blog]  # => Home / Blogs / My blog

Note on Patches/Pull Requests

Patches/Pull Requests are welcome!

  • Fork the project.

  • Make your feature addition or bug fix.

  • Commit.

  • Send a pull request.

Copyright © 2010 Dmitry Naumov. Released under the MIT license. See LICENSE for details.