【问题标题】:How build a breadcrumbs in rails 4如何在rails 4中构建面包屑
【发布时间】:2016-06-08 15:51:37
【问题描述】:

说真的,我不知道从哪里开始。如何在不使用 gems 的情况下实现辅助面包屑? 我尝试了一些宝石,但我更喜欢做一个简单的帮助。存在某人或一些教程吗?我没找到这个=/

谢谢!

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-4 helper breadcrumbs rails-4-2-1


【解决方案1】:

我的解决方案:

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 %>

还有任何观点:

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

【讨论】:

  • 不错的解决方案,我用它自动制作面包屑,请查看我的答案。
【解决方案2】:

你不能这样做。

在您的 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

现在在视图中粘贴以下内容:(我使用了 index_path 和 @user.name)- 作为示例,您可以将此代码粘贴到显示视图中

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

现在,当您需要一些面包屑时,您只需调用上面的这个主干并更改路径和实例变量@your_variable

【讨论】:

    【解决方案3】:

    我进一步研究了 Elton Santos 的解决方案,并决定面包屑应该像历史一样是自动的。所以我修改了一些代码:

    在我的 application.html.erb 中

    <%= render_navigation %>
    

    在我看来,我已经在使用了:

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

    所以,我的 navigation_helper.rb 看起来像:

    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
    

    最后是 _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>
    

    所以,这里发生了什么;我将每个页面标题保存在会话中并从中构建面包屑。我只保留最近的三个条目以及 Home 的硬编码条目,并在它们不分开时删除重复的条目。

    【讨论】:

    • 但是,如果我在两个(或更多)浏览器选项卡中打开您的网站并同时使用它们,那会搞砸,不是吗?
    • Nav/Breadcrumbs 将受到不同选项卡的影响,但其方式可能为大多数人所接受。页面访问实际上会反映在所有选项卡上,您可以将其称为功能。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多