Search code examples
ruby-on-rails-3breadcrumbs

breadcrumbs_on_rails rendering twice


I am trying to get my breadcrumbs to follow my navigation history through different controllers

Application Controller

add_breadcrumb 'Home', root_path

In my public_pages controller

class PublicPagesController < ApplicationController


def index

end

def news
 add_breadcrumb "News", news_path
 add_breadcrumb "Contact us", contact_path
end

def contact_us
add_breadcrumb "News", news_path
add_breadcrumb "Contact us", contact_path
end

so i have another controller called private_pages which is only accessible when a user logs in, and this has its own root_path,

How would i show breadcrumbs when accessing different actions in different controllers

Thanks


Solution

  • First, add the home breadcrumb to your ApplicationController as that should be registered for every request. If your application is not publicly accessible in this regard, then disregard that, and keep the home breadcrumb in your PublicPagesController before the methods.

    Then, update your PublicPagesController:

    class PublicPagesController < ApplicationController
    
      def index
      end
    
      def news
        # to show Home / Contact Us / News
        add_breadcrumb "Contact Us", news_path
        add_breadcrumb "News", news_path
      end
    
      def contact_us
        add_breadcrumb "Contact Us", news_path
      end
    
    end
    

    The above assumes that add_breadcrumb "Home", news_path is called in your ApplicationController.

    In regards to bootstrap conflicts or integration, see these two:

    https://github.com/weppos/breadcrumbs_on_rails/issues/24
    https://gist.github.com/2400302

    If you want to amend the home breadcrumb based on whether user is logged in or not, add a before_filter to your ApplicationController:

    before_filter :set_home_breadcrumb
    
    def set_home_breadcrumb
      if user_signed_in?
        add_breadcrumb "Home", :user_home_path
      else
        add_breadcrumb "Home", :root_path
      end
    end