How do I create a dynamically generated breadcrumb in Rails 3?

后端 未结 2 709
抹茶落季
抹茶落季 2020-12-23 12:49

Breadcrumb being the navigational element that is used to tell the user where they are on the site.

E.g.

Home >> Projects >> Stages >> Uploads.

Where

2条回答
  •  眼角桃花
    2020-12-23 13:34

    I use almost the same code for like 10 years... Wrote it first in ASP, then C#, PHP, and now Rails:

    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 => 'shared/navigation', :locals => { :nav => ensure_navigation }
        end
    end
    

    Then, in shared/navigation.html.erb:

    <% nav.each do |n| %> <%= link_to n[:title], n[:url] %> <% end %> <%= link_to yield(:title), request.path, :class => 'no-link' %>

    Your regular view:

    <% content_for :title, 'My Page Title' %>
    <% navigation_add 'My Parent Page Title', parent_page_path %>
    

    And your template:

    
    
       <%= yield :title %> 
    
    
      ...
      <%= render_navigation %>
      ...
      <%= yield %>
    
    
    

提交回复
热议问题