Easy breadcrumbs for Rails apps.
-
Include
Croutons::Controllerin yourApplicationController.This will make a
#breadcrumbshelper available in your layouts and views. -
Call the
#breadcrumbshelper in your layouts or views. -
Define a
BreadcrumbTrailclass, which inherits fromCroutons::BreadcrumbTrail. -
Define missing methods on the
BreadcrumbTrailclass.For example, for the
admin/locations/index.html.erbview you would define an#admin_locations_indexmethod.In these methods, you build up a breadcrumb trail by calling
#breadcrumbwith a label and an optional URL. You can also call previously defined methods to build on existing trails. View assigns (i.e. the controller instance variables) are available via the#objectsmethod which returns aHash. Rails route helpers are also available inside this class.
Please see the example below for further reference.
-
Instead of defining a
BreadcrumbTrailclass you can use an object of your own that responds to#breadcrumbs.To do this, override the private
#breadcrumb_trailmethod in the controller where you includedCroutons::Controller, to return the object you want to use.The
#breadcrumbsmethod is passed two parameters: onetemplate_identifierStringand oneobjectsHash. The#breadcrumbsmethod should return anArrayofCroutons::Breadcrumbs. -
Override the view used to render breadcrumbs.
To do this, create a view called
breadcrumbs/_breadcrumbs.html.erb.In this view, an
ArrayofCroutons::Breadcrumbs is assigned to the local variablebreadcrumbs. TheseCroutons::Breadcrumbs have two public attributes:#labeland#url. The#urlattribute is optional. To check whether theCroutons::Breadcrumbhas a#urlor not (i.e. should be rendered as a link or not), check whether the#link?method returnstrueorfalse.
class ApplicationController < ActionController::Base
include Croutons::Controller
end
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
end
<!DOCTYPE html>
<html>
<head>
<title>My blog</title>
</head>
<body>
<%= breadcrumbs %>
<%= yield %>
</body>
</html>
class BreadcrumbTrail < Croutons::BreadcrumbTrail
def posts_index
breadcrumb("Posts", posts_path)
end
def posts_show
posts_index
breadcrumb(objects[:post].title, post_path(objects[:post]))
end
end
Croutons is Copyright © 2016 Calle Erlandsson, George Brocklehurst, and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.
Croutons is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.
We love open source software! See our other projects. We are available for hire.