Skip to content

Managing app breadcrumbs with Para's breadcrumbs system

Valentin Ballestrino edited this page Apr 20, 2017 · 1 revision

You can add breadcrumbs in your app easily with the para breadcrumbs API.

  1. Build breadcrumbs
  2. Display breadcrumbs in your app
  3. Override breadcrumbs view

1. Build breadcrumbs

In your controllers or views, you can concatenate breadcrumbs data by using the following #add_breadcrumb.

You'll often want to add the :home breadcrumb to your ApplicationController to allow users to always access the app root.

class ApplicationController < ActionController::Base
  add_breadcrumb :home, :root_path
end

Then in your controller actions, add collection and member level breadcrumbs :

class PostsController < ApplicationController
  add_breadcrumb :posts # Automatically maps to posts_path

  def show
    @post = Post.find(params[:id])

    add_breadcrumb(@post)
  end
end

The above are the shorter ways of using the helper. Here are all the ways of using the helper :

# Automatically maps to `posts_path` and displays the Post model plural translation or t('breadcrumbs.posts')
add_breadcumb :posts 

# Gets the breadcrumb name from the resource (`#name`, `#title` or other) and builds the path with post_path(@post)
add_breadcumb @post

# Uses t('breadcrumbs.posts') translation and gets the explicit route passed as second argument
add_breadcumb :posts, category_posts_path(@category)

# Uses the provided name and route
add_breadcumb @post.title, category_post_path(@category, @post)

2. Display breadcrumbs in your app

In your layout (or anywhere you'll like to add them), just call the render_breadcrumbs helper

= render_breadcrumbs

3. Override breadcrumbs view

Just copy the view from the repo here to app/views/shared/_breadcrumbs.html.haml and override as needed.

Clone this wiki locally