0

说真的,我不知道从哪里开始。如何在不使用宝石的情况下实施助手面包? 我尝试了一些宝石,但我preffer做一个简单的帮助。存在某人或某个教程?我没有找到这个=/如何在导轨上制作面包屑4

谢谢!

+1

我希望这有助于:http://stackoverflow.com/questions/4289272/how-do-i-create-a-dynamically-generated-breadcrumb-in-rails-3 – akz92

回答

2

我的答案:

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

好吗?喜欢?给我一票!

+0

好的解决方案,我用它来制作自动检查面包屑,请检查我的答案。 – Ghazi

1

你不能这样做。

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

现在当你需要一些面包屑时,你可以直接调用上面的这个trunck,改变路径和实例变量@your_variable

0

我进一步研究了埃尔顿桑托斯的解决方案,并决定面包屑应该像历史一样自动化。所以我修改一些代码:

在我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条目,并在它们没有分开时删除重复的条目。