How build a breadcrumbs in rails 4

2.5k views Asked by At

Seriously, I have no idea where to start. How do I implement a helper breadcrums without using gems? I tried some gems, but I preffer make a simple helpe. Exist someone or some tutorial? I not found this =/

Thanks!

3

There are 3 answers

1
Elton Santos On BEST ANSWER

My solution:

navigation_helper.rb

module NavigationHelper
  def ensure_navigation
    @navigation ||= [ { :title => 'Home', :url => '/' } ]
  end

  def navigation_add(title, url)
    ensure_navigation << { :title => title, :url => url }
  end

  def render_navigation
    render :partial => 'navigation', :locals => { :nav => ensure_navigation }
  end
end

_navigation.html.erb

  <ol class="breadcrumb">
    <% nav.each do |n| %>
      <% unless n.equal? nav.last %>
        <li><%= link_to n[:title], n[:url] %></li>
      <% else %>
        <li><%= n[:title] %></li>
      <% end %>
    <% end %>
  </ol>

application.html.erb

<%= render_navigation %>

And any view:

<% content_for :title, 'My Page Title' %>
    <% navigation_add @something.anything, '#' %>
0
Stevan O. On

You cant do this.

In your application_helper:

def breadcrumb(&block)
    content_tag :ol, { :class => "breadcrumb", :itemprop => "breadcrumb" } do
      block.call
    end
end

def breadcrumb_item(name = nil, url = nil, html_options = {}, &block)
    if name or block
      html_options[:class] = "#{html_options[:class]} active" unless url
      content_tag :li, html_options do
        if block
          block.call name, url
        else
          url ? link_to(name, url) : name
        end
      end
    end
  end

Now in views you paste this: (I used index_path and @user.name) - you can paste this code on show view as an example

<%= breadcrumb do %>
  <%= breadcrumb_item "index", index_path %>
  <%= breadcrumb_item @user.name %>
<% end %>

Now when you need some breadcrumb you can just call this trunck above and change the path and the instance variables @your_variable

2
Simptive On

I further worked on Elton Santos's solution and decided breadcrumbs should be automatic like history. So I modified some code:

In my application.html.erb

<%= render_navigation %>

In my views, I was already using:

<% content_for :heading do 'User Detail' end %>

So, my navigation_helper.rb look like:

module NavigationHelper
  def navigation_add(title, url)
    nav_list = session['navigation'].present? ? session['navigation'] : []
    nav_list << { 'title' => title, 'url' => url }
    # 1. Take last 3 items only (-1 is last, not -0)
    nav_list = nav_list[-3..-1] if nav_list.length > 3
    # 2. Now, if first is pointing root, remove it
    nav_list.shift if nav_list[0]['url'] == '/'
    # 3. If last one is same as its predecessor, remove it
    nav_list.pop if nav_list.length > 1 && (nav_list[-1]['url'] == nav_list[-2]['url'])

    session['navigation'] = nav_list
  end

  def render_navigation
    render partial: 'shared/navigation', locals: { nav_list: session['navigation'] }
  end
end

and finally, _navigation.html.erb:

<ol class="breadcrumb">
  <li><%= link_to '/' do %>
      <i class="fa fa-home"></i> Home <% end %>
  </li>
  <i class="fa fa-angle-double-right" style="color: #ccc; padding: 0 5px;"></i>

  <% nav_list.each_with_index do |nav, i| %>
    <% if i != nav_list.length-1 %>
      <li><%= link_to nav['title'], nav['url'] %></li>
    <% else %>
      <li class="active"><%= nav['title'] %></li>
    <% end %>
  <% end %>
</ol>

So, what's going up here is; I save every page title in session and build breadcrumbs from that. I keep recent three entries only along with hard-coded one for Home and remove duplicate entries when they are not apart.