相关疑难解决方法(0)

在Rails 3中为nav添加"当前"类的最佳方法

我在导航菜单中有一些静态页面.我想在当前显示的项目中添加类似"当前"的类.

我这样做的方法是添加大量的辅助方法(每个项目一个)来检查控制器和操作.

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)

有没有更好的方法呢??我目前的方式是如此愚蠢......

navigation ruby-on-rails-3

110
推荐指数
6
解决办法
6万
查看次数

如何以DRY方式基于current_page将"活动"类应用于导航? - Rails 3

所以在我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看起来像这样: …

ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

3
推荐指数
1
解决办法
3349
查看次数