2010-11-26 102 views

回答

25

我使用几乎像10年相同的代码...在ASP先写,然后C#,PHP,现在扶手:

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 

然后,在shared/navigation.html.erb

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

您的常规看法:

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

而且你的模板:

<html> 
<head> 
    <title> <%= yield :title %> </title> 
</head> 
<body> 
    ... 
    <%= render_navigation %> 
    ... 
    <%= yield %> 
</body> 
</html> 
+0

这是很干爽更新!谢谢。将检查它,以确保它适用于我。谢谢! – marcamillion 2010-11-27 08:53:30

1

前面的答案可以在不使用<% content_for :title, 'My Page Title' %>

<div class="history-nav"> 
    <% nav.each do |n| %> 
     <% unless n.equal? nav.last %> 
     <%= link_to n[:title], n[:url] %>    
     <% else %> 
     <%= n[:title] %> 
     <% end %> 
    <% end %> 
    </div>