# Crumbly
Crumbly builds breadcrumb trails for Rails apps. A breadcrumb trail is the line of links at the top of a page that
shows where it sits in the site:
```
Home › Customers › Alice › Orders › Order 2026-0042
```
You describe each level once, as a Ruby method that names its parent. Most pages need nothing more: when a view
doesn't say which trail it wants, Crumbly works it out from the controller, the action and the routes. A test
checks every trail in the app, so a broken one fails CI before anyone opens the page.
The guide, the migration notes and the benchmarks are also on the website: .
Coding agents can start from , or read this README and the
website's pages as one Markdown file: .
## Why Crumbly
Crumbly was written to replace [gretel](https://github.com/kzkn/gretel) in a back office with 265 crumbs, in one
file of about 1,000 lines, and 380 calls in views. It works differently in a few ways:
- The crumbs of each part of the app live in their own class, `Admin::Breadcrumbs` or `Storefront::Breadcrumbs`,
so the admin and the storefront can both have an `orders` crumb.
- A crumb is a plain method. Route helpers work in it as in a view, and a backtrace points at its line.
- Index, show, new and edit pages get their trail from the routes and the record the controller loaded, so most
views name no crumb.
- One test reads every crumb and fails on a parent, a route helper or a crumb name that doesn't exist, without
touching the database.
- The trail comes back as data, and your layout writes the HTML. The gem has no styles or rendering options.
- Rendering a five-level trail as an `` takes 45.4 µs against 57.6 µs for gretel, with 274 allocations
instead of 345 (YJIT off). JSON-LD is a little slower: see [Performance](https://github.com/levups/crumbly#performance).
Its runtime dependencies are Action Pack, Active Support and Loofah, which Rails already installs.
## Installation
Add the gem to your Gemfile:
```ruby
gem "crumbly"
```
It needs Ruby 3.3 or later and Rails 8.0 or later. There is nothing to configure: the gem adds `breadcrumb`,
`breadcrumb_trail` and `breadcrumb_json_ld` to your views, and `breadcrumbs_with` to your controllers.
## Getting started
### 1. Write the crumbs
Crumbs live in a class that inherits from `Crumbly::Trail`. Every public method in it is a crumb. A crumb returns
a label and a URL, and its parent if it has one:
```ruby
# app/breadcrumbs/admin/breadcrumbs.rb
module Admin
class Breadcrumbs < Crumbly::Trail
def root
crumb "Home", admin_root_path
end
def orders
crumb "Orders", admin_orders_path
end
def order(order)
crumb order.to_s, admin_order_path(order), parent: orders
end
end
end
```
The parent of `order` is the result of calling `orders`, so the trail of an order page is Home › Orders ›
Order 42. When `root` exists, every trail starts with it. Your route helpers work inside the class as they do in
a view. Rails loads the file like any other in `app/`, so `app/breadcrumbs/admin/breadcrumbs.rb` defines
`Admin::Breadcrumbs`.
### 2. Render the trail in the layout
`breadcrumb_trail` returns the levels of the current page, from the root to the page itself. Each level has a
`label`, a `url` and `current?`, which is true for the last one. The HTML is up to you:
```erb
<%# app/views/layouts/admin.html.erb %>
```
On the page of an order whose `to_s` is "Order 42", with the admin routes at `/admin`, this writes:
```html
```
### 3. Pick a crumb in a view, when you need to
A view can name the crumb it wants:
```erb
<%# app/views/admin/orders/show.html.erb %>
<% breadcrumb @order %>
```
`breadcrumb @order` calls the crumb named after the record's model, `order` for an `Order`. To call a crumb by
name, write `breadcrumb :orders`, and pass its arguments after the name (`breadcrumb :order, @order`).
In this example the line isn't needed. A `show` page with no `breadcrumb` call already gets the trail of its
record (see [Trails Crumbly works out on its own](https://github.com/levups/crumbly#trails-crumbly-works-out-on-its-own)).
## Writing crumbs
A crumb calls `crumb label, url = nil, parent: nil, links: []`.
`url` can be a string, a record or a polymorphic array such as `[:admin, owner, :returns]`. Leave it out for a
level without a link. A `javascript:`, `vbscript:` or `data:` URL would run a script once your layout links it,
so Crumbly refuses it, however it is spelled (`" JavaScript:"`, `"java\tscript:"`, or `"javascript:"` in an
`html_safe` string, which the browser decodes): see
[Mistakes in development and in production](https://github.com/levups/crumbly#mistakes-in-development-and-in-production).
`links:` is for a level that points to more than one page. It takes extra `link(label, url)` entries, which your
layout reads from `level.links`.
`crumb_for(record)` finds the crumb of a record from its model. Use it when the parent can be one of several
models:
```ruby
def returns(owner = nil)
return crumb("Returns", admin_returns_path) unless owner
crumb "Returns", [:admin, owner, :returns], parent: crumb_for(owner)
end
```
For a single-table inheritance subclass that has no crumb of its own, `crumb_for` and `breadcrumb @record` use the
crumb of its parent class.
Private methods are helpers, not crumbs, so you can put shared code in them. View helpers such as `t` or
`current_user` are reached through `helpers`: `helpers.t(".orders")`.
Any public method can be called from a view, so `Crumbly::Trail` keeps its own methods private. Don't name a crumb
`crumb`, `link`, `crumb_for` or `helpers`.
## One trail class per namespace
`Admin::OrdersController` uses `Admin::Breadcrumbs`. If there is none, Crumbly looks in the enclosing modules,
then for a top-level `Breadcrumbs`. An admin area and a storefront can each have an `orders` crumb, and they
don't clash.
A controller outside the namespace picks its class with `breadcrumbs_with`:
```ruby
class CheckoutController < ApplicationController
breadcrumbs_with Storefront::Breadcrumbs
end
```
## Trails Crumbly works out on its own
When a view doesn't call `breadcrumb`, Crumbly builds the trail from the action:
| Action | Trail |
|---|---|
| `index` | the collection: your `posts` crumb if you wrote one, or else the model's plural name linked to the `index` route |
| `show` | the record: your `post` crumb, or else its label linked to the `show` route, under the collection |
| `new`, `create` | "New" under the collection |
| `edit`, `update` | "Edit" under the record |
The record is the one the controller has already loaded, `@post` in `PostsController`. Crumbly never runs a query
to find it. "New" and "Edit" come from the `crumbly.new` and `crumbly.edit` translations, so you can translate
them. A record's label is its `to_s`. A model without a `to_s` of its own gets its translated model name and id
instead ("Post 5"), not `#`.
Nested routes work too. On `/admin/posts/5/comments`, the trail is Posts › Post 5 › Comments, and the post is
the `@post` the controller loaded. If the controller didn't load it, Crumbly skips that level and keeps Posts.
A singular resource with a `show` route (`resource :account`) has no collection: its record links to that route,
under its parent record when the route is nested, so the profile on `/admin/customers/7/profile` sits under its
customer. A crumb named after the controller or the nesting (`customer_profiles`) still comes first.
To change how these trails are built, override the private methods `collection_crumb`, `record_crumb`,
`new_record_crumb` or `edit_record_crumb` in your trail class.
To turn them off, set `derive_trails false` in the trail class. A view that doesn't call `breadcrumb` then has no
trail, as with gretel: `breadcrumb_trail` returns `[]` and `breadcrumb_json_ld` returns `nil`.
```ruby
module Admin
class Breadcrumbs < Crumbly::Trail
derive_trails false
end
end
```
Subclasses inherit the setting, and `derive_trails true` turns it back on in one of them.
### Which crumb wins
Crumbly takes the first of these that applies:
1. The crumb the view names with `breadcrumb`.
2. Your crumb named after the controller: `posts` in `Admin::PostsController#index`. Under a nested route it is
named after the whole nesting and gets the parent records, so `/admin/orders/42/messages` calls
`order_messages(order)`. On `show` and `edit`, it is the crumb named after the record's model (`post`).
3. The trail Crumbly works out. Its parent levels follow the same rule: your crumb for the parent's model if there
is one, or else a level Crumbly builds.
With `derive_trails false`, only the first applies.
## Testing your trails
Add one test:
```ruby
# test/breadcrumbs_test.rb
require "test_helper"
require "crumbly/test_helper"
class BreadcrumbsTest < ActiveSupport::TestCase
test "breadcrumbs" do
assert_breadcrumbs_valid
end
end
```
It loads the whole app and reads the source of every crumb, without running them or touching the database. It
fails when:
- a crumb's parent is not a crumb, or a `_path` or `_url` it calls is not a route helper;
- a `breadcrumb :name` in `app/views` or `app/controllers` names a crumb that no trail class defines;
- a view or another crumb calls a crumb with the wrong number of arguments (calls spread over several lines, or
with a splat, are skipped);
- a crumb is never used. A crumb is used when another crumb, a view or a controller calls it, when a trail
Crumbly works out reads it (unless its class sets `derive_trails false`), or when it is named after a model.
A crumb defined twice in the same class is caught even earlier: the class raises `Crumbly::DuplicateCrumb` when it
loads.
Outside Minitest, `Crumbly.verify!` runs the same checks. It raises `Crumbly::InvalidTrail` with every problem in
one message, and returns the unused crumbs (`["Admin::Breadcrumbs#drafts"]`). Both accept `trail_classes:`,
`routes:` and `sources:`, the directories it scans for `breadcrumb` calls.
## Mistakes in development and in production
A view that asks for a crumb that doesn't exist raises `Crumbly::UnknownCrumb` in development and test. So does
`breadcrumb @order` when `@order` is `nil`. In production the error goes to `Rails.error`, and the page renders
without a trail instead of failing.
A crumb whose URL a browser would run as a script raises `Crumbly::UnsafeUrl` in development and test. In
production the error goes to `Rails.error`, and the level keeps its label without the link.
To raise in production too, on a staging server for example, set either in an initializer:
```ruby
# config/initializers/crumbly.rb
Crumbly.raise_on_unknown_crumb = true # on staging too
Crumbly.raise_on_unsafe_url = true
```
## Breadcrumbs for search engines
Google can show a page's trail in its results when the page describes it as schema.org structured data.
`breadcrumb_json_ld` writes that description for the current trail. Put it in the ``:
```erb
<%# app/views/layouts/storefront.html.erb %>
<%= breadcrumb_json_ld %>
```
Google needs absolute URLs, so use `_url` helpers in the crumbs of that namespace:
```ruby
module Storefront
class Breadcrumbs < Crumbly::Trail
def category(category)
crumb category.name, storefront_category_url(category),
parent: category.parent ? category(category.parent) : shop
end
end
end
```
The levels Crumbly builds for a trail it works out get full URLs in the JSON-LD on their own, and keep paths in
the page. Your own crumbs, `root` included, still need `_url` helpers.
A relative URL raises `Crumbly::RelativeUrl` in development and test. In production it is reported to
`Rails.error` and the page has no structured data. `Crumbly.raise_on_relative_url` changes this.
Some details of the output:
- Positions start at 1. The last level may have no URL. A level above it without a URL isn't a page, so it is
left out.
- Names are plain text. If a label is `html_safe` (an icon, a ``), its tags are removed.
- The JSON is escaped for a `