0

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!

Elton Santos
  • 571
  • 6
  • 32
  • 1
    I hope this helps: http://stackoverflow.com/questions/4289272/how-do-i-create-a-dynamically-generated-breadcrumb-in-rails-3 – Lucas Moulin Feb 25 '16 at 17:55

3 Answers3

2

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, '#' %>
Pang
  • 9,564
  • 146
  • 81
  • 122
Elton Santos
  • 571
  • 6
  • 32
1

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

Stevan O.
  • 161
  • 1
  • 8
1

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.

Ghazi
  • 966
  • 8
  • 15
  • But that will mess up if I have your site opened in two (or more) browser tabs and work with them simultaneously, won't it? – Ngoral Dec 18 '19 at 12:58
  • Nav/Breadcrumbs will be affected by different tabs but in a way which might be acceptable by most. Page visits will actually reflect on all tabs and you may call it a feature. :) – Ghazi Dec 18 '19 at 14:53