set class =' active'使用current_page进行索引和显示操作

时间:2016-08-25 19:38:04

标签: ruby-on-rails-4

这应该有效,但不是......我想为文章的索引和节目动作设置导航链接的样式。

<li class="<%= "active" if current_page?(articles_path(@article))  %>"><%= link_to '/articles' do %>
                       <span>articles</span><span class="rd-navbar-label text-middle label-custom label-danger label-xs-custom label-rounded-custom label">new</span>
                      <% end %>
                      </li>

当路径为/ articles OR / articles / 5时,应该应用Active类,但仅适用于/ articles。

我试过<%= "active" if current_page?(controller: 'articles', action: 'index') || current_page?(controller: 'articles', action: 'show') %> 但是当另一个链接使用不同的控制器时,这会导致No路由匹配{:action=>"show", :controller=>"articles"}

这应该很简单......怎么样?

以下是所有导航栏代码...当我们查看时,如果在网址为/#solutions

时将如何生成锚点的链接
<ul class="rd-navbar-nav">
                      <li class="<%= "active" if current_page?(root_path) %>"><%= link_to "home", root_path %></li>

                      <li><%= link_to "Solutions", root_url(:anchor => "solutions") %></li>

                      <li class="<%= "active" if current_page?(contact_path) %>"><%= link_to "Contact", contact_path %></li>
                      <li class="<%= "active" if current_page?(faq_path) %>"><%= link_to "faq", faq_path %></li>

                        <li class="<%= "active" if current_page?(controller: 'articles', action: 'index') || current_page?(controller: 'articles', action: 'show') %>"><%= link_to '/articles' do %>
                       <span>articles</span><span class="rd-navbar-label text-middle label-custom label-danger label-xs-custom label-rounded-custom label">new</span>
                      <% end %>
                      </li>
                    </ul>

1 个答案:

答案 0 :(得分:1)

您是否尝试过将字符串与controller.controller_name controller.controller_name == 'articles'进行比较?

<li class="<%= "active" if controller.controller_name == 'articles' %>"><%= link_to '/articles' do %><span>articles</span><span class="rd-navbar-label text-middle label-custom label-danger label-xs-custom label-rounded-custom label">new</span>