我在导航菜单中有一些静态页面.我想在当前显示的项目中添加类似"当前"的类.
我这样做的方法是添加大量的辅助方法(每个项目一个)来检查控制器和操作.
def current_root_class
'class="current"' if controller_name == "homepage" && action_name == "index"
end
<ul>
<li <%= current_root_class %>><%= link_to "Home", root_path %>
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢??我目前的方式是如此愚蠢......
所以在我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>
Run Code Online (Sandbox Code Playgroud)
但是,我想要做的是,一旦它们出现在导航中的任何页面上,它就会将类应用于active相应的链接.
因此,例如,如果用户在mydomain.com/johnbrown哪个Profile链接上,则rails helper看起来像这样: …