Created
June 21, 2012 22:47
-
-
Save bricker/2969085 to your computer and use it in GitHub Desktop.
Simple breadcrumbs for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Simple breadcrumbs for Rails --> | |
<!-- Example output --> | |
<!-- This example uses the Twitter Bootstrap classes for breadcrumbs --> | |
<!-- http://twitter.github.com/bootstrap/components.html#breadcrumbs --> | |
<% if breadcrumbs %> | |
<ul class="breadcrumb"> | |
<% breadcrumbs.each do |crumb| %> | |
<li> | |
<%# You could even turn these next 5 lines into a helper, | |
`link_or_else_title(crumb[:title], crumb[:link])` or something. %> | |
<% if crumb[:link].present? %> | |
<%= link_to crumb[:title], crumb[:link] %> | |
<% else %> | |
<%= crumb[:title] %> | |
<% end %> | |
<% unless crumb == breadcrumbs.last %> | |
<span class="divider">/</span> | |
<% end %> | |
</li> | |
<% end %> | |
</ul> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple breadcrumbs for Rails | |
# A very easy and elegant way to add breadcrumbs | |
# to your Rails application. | |
# This gist is adapted from my usage of it so I apologize if there are mistakes. | |
class ApplicationController < ActionController::Base | |
# This before_filter adds a root node to the breadcrumbs for | |
# every request. Only necessary if you want a "Home" link, for | |
# example. | |
before_filter :root_breadcrumb | |
def root_breadcrumb | |
breadcrumb "Home", root_path | |
end | |
def breadcrumb(*args) | |
@breadcrumbs ||= [] | |
# Grab pairs from the arguments and convert to hash | |
pairs = args.each_slice(2).map { |pair| { title: pair[0], link: pair[1] } } | |
# Push each hash into @breadcrumbs | |
pairs.each { |pair| @breadcrumbs.push(pair) } | |
end | |
# Setup a helper method for reading the breadcrumbs. | |
# Not necessary, but makes the views look a little cleaner. | |
# Replace `breadcrumbs` with `@breadcrumbs` in the views if | |
# you don't use this helper method. | |
attr_reader :breadcrumbs | |
helper_method :breadcrumbs | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple breadcrumbs for Rails | |
# Example usage in your normal controllers. | |
class BlogEntriesController < ApplicationController | |
before_filter :get_blog_entry, only: [:show, :edit, :update, :destroy] | |
before_filter :resource_breadcrumb | |
def index | |
# Breadcrumb already created by before_filter | |
end | |
def new | |
# A link is not required. Leave it off if you don't need it. | |
breadcrumb "New Blog Entry" | |
end | |
def show | |
breadcrumb @blog_entry.title, blog_entry_path(@blog_entry) | |
end | |
def create | |
# You can put a breadcrumb here but it will only be seen | |
# if there is an error on the form. | |
# You can append more than one breadcrumb at a time, but | |
# remember it's grouped every two, so if you don't want a | |
# link on one, pass "nil" in between. | |
breadcrumb "New Blog Entry", nil, "Error!" | |
end | |
# `update` & `destroy` are similar to `create` | |
protected | |
def get_blog_entry | |
if !@blog_entry = BlogEntry.find_by_id(params[:id]) | |
redirect_to blog_entries_path and return false | |
end | |
end | |
def resource_breadcrumb | |
breadcrumb "Blog Entries", blog_entries_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment