ruby-on-railsruby-on-rails-4helperbreadcrumbsrails-4-2-1

How build a breadcrumbs in rails 4


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!


Solution

  • 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, '#' %>