1

我有一个名为分类单元的类别列表:

Rails 代码在这里:

<div id="taxonomies" class="sidebar-item">
<% @taxonomies.each do |taxonomy| %>
  <ul class="navigation-list">

      <% taxonomy.root.children.each do |taxon| %>
        <li<%= ' class="current"' if @taxon and ([@taxon] + @taxon.ancestors).include?(taxon) %>><%= link_to taxon.name, seo_url(taxon) %></li>

            <% if @taxon.nil? %>
            <% else %>

                <% @taxon.children.each do |taxon|%>
                    <li><%= link_to taxon.name, seo_url(taxon) %></li>
                <% end %>

            <% end %>
      <% end %>
    </ul>
<% end %>
</div>

现在的问题是,如果我单击视图中的特定分类,它会向我显示所有父母的孩子。所以...

类别 1 后备 1 后备 2 类别 2 后备 1 后备 2 类别 3 后备 1 后备 2

当我想要的是:类别 1 Decendent 1 Decendent 2 Category 2 Category 3

Selected 类别具有 css 类 current 附加到它。我的想法是某种 <% if :class => 'current %> 显示 LI,否则什么也不显示。

有谁知道这是否可能?

4

2 回答 2

0

在控制器上添加即时变量。喜欢

def blablabla
  @current_page = Category.name
  if params[:id => @current_page]
    @current = "current"
  else
    @current = ""   
  end
end

并在这里改变

<li<%= ' class="#{@current}"' if @taxon and ([@taxon] + @taxon.ancestors).include?(taxon) %>><%= link_to taxon.name, seo_url(taxon) %></li>
于 2009-12-05T07:52:27.730 回答
0

您不能重用相同的条件来添加类属性吗?

if @taxon and ([@taxon] + @taxon.ancestors).include?(taxon)

所以你会有:

<% taxonomy.root.children.each do |taxon| %>
<li<%= ' class="current"' if @taxon and ([@taxon] + @taxon.ancestors).include?(taxon) %>><%= link_to taxon.name, seo_url(taxon) %></li>

    <% if @taxon and ([@taxon] + @taxon.ancestors).include?(taxon) %>

        <% @taxon.children.each do |taxon|%>
            <li><%= link_to taxon.name, seo_url(taxon) %></li>
        <% end %>

    <% end %>
<% end %>
于 2009-12-04T22:59:36.293 回答