所以在我的application.html.erb
我的导航结构看起来像这样:
<div id="navigation">
<ul class="pills">
<% if current_page?(:controller => 'welcome', :action => 'index') %>
<li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
<li><%= link_to "Settings", settings_path %></li>
<li><%= link_to "Sign Out", signout_path %></li>
<% elsif !current_page?(:controller => 'welcome', :action => 'index') %>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
<li><%= link_to "Settings", settings_path %></li>
<li><%= link_to "Sign Out", signout_path %></li>
<% end %>
</ul>
</div>
但是,我想做的是,一旦它们位于导航中的任何页面上,它就会应用一个类active
到相应的链接。
例如,如果用户处于开启状态mydomain.com/johnbrown
哪一个是Profile
链接,rails 助手看起来像这样:
link_to "Profile", vanity_path(:vname => current_user.username), :class => "active"
.
但是我如何以编程方式做到这一点,这样我就不会重复内容?即如何为导航中的所有页面获取该功能并尽可能将其编写为 DRY?
Thanks.