Breadcrumb
Instalation
Put the following line in your Gemfile
gem "breadcrumb"
Usage
Layout:
<%= breadcrumb %>
You can add a root path by doing this
<%= raw breadcrumb.prepend("Home","/") %>
In your view (say the uri path is /books/religion/the-holly-bible):
“‘ruby
<%
breadcrumb.add("Books","/books")
breadcrumb.add("Religion", books_category_path(@book.category))
breadcrumb.add(@book.title)
%>
“‘
It will render
“‘html
<div class="breadcrumb">
<a href="/books">Books</a>
<span class="separator"> » </span>
<a href="/books/religion">Religion</a>
<span class="separator"> » </span>
<span>The Holly Bible</span>
</div>
“‘
You can change the separator
“‘ruby
.separator = '<span class="separator"> > </span>'
“‘
Titleizing You can use breadcrumb for creating the page title:
“‘html
<title><%= breadcrumb.titleize %></title>
“‘
which will ouput
“‘html
<title>Books :: Religion :: The Holly Bible</title>
“‘
Titleize accepts block. When using blocks, returning false removes the title part. Returing nil, won’t change the behavior. Returning String changes the title part.
“‘html
<title><%= breadcrumb.titleize {|part| "Our Awesome Books" if parts == 'Books' } %></title>
“‘
Ouputs
“‘
<title>Our Awesome Books :: Religion :: The Holly Bible</title>
“‘
Removing the title part
“‘
<title><%= breadcrumb.titleize {|part| false if part == "Religion" } %></title>
<!– or –>
<title><%= breadcrumb.titleize {|part, order| false if order == 1 } %></title>
```
Outputs
“‘html
<title>Books :: The Holly Bible</title>
“‘
And you can change the title separator:
“‘ruby
.title_separator = '-'
“‘