Я использую почти один и тот же код около 10 лет ... Сначала написал его в ASP, затем в C #, PHP, а теперь и в 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
Затем в 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>