2012-01-21 7 views
0

私はしばらくこのことを把握しようとしています。私は、そのページにあるときに、リストアイテムが "アクティブ"のクラスを取得するナビゲーションを持っています。私はまた、それぞれのユニークなクラスのリスト項目のアンカーを与える必要があります。だから私のコードは次のとおりです。クラスを既にliクラスでアンカーに追加する "current"

Application_helper

def link_to_with_current_class(name, options) 
    if current_page?(options) 
    content_tag :li, link_to(h(name), options), :class => "current" 
    else 
    content_tag :li, link_to(h(name), options) 
    end 
end 

ナビゲーション

<ul id="nav"> 
     <%= link_to_with_current_class "work", home_path %> 
     <%= link_to_with_current_class "about", about_path %> 
     <%= link_to_with_current_class "contact", contact_path %> 
    </ul> 

これは、私はそれが

<ul id="nav"> 
     <li class="current"><a **class="work"** href="/">work</a></li> 
     <li><a **class="about"** href="/about">about</a></li> 
     <li><a **class="contact"** href="/contact">contact</a></li> 
    </ul> 
を生成する必要が

<ul id="nav"> 
     <li class="current"><a href="/">work</a></li> 
     <li><a href="/about">about</a></li> 
     <li><a href="/contact">contact</a></li> 
    </ul> 

を生成します210

私はこれを多くの方法で試しましたが、試してみると分かりません。この問題についての助けがあれば、大いに手当てされます。

答えて

1

それがうまくいくかもしれないようにこれが見えます:迅速な対応のための

def link_to_with_current_class(name, options) 
    if current_page?(options) 
    content_tag :li, link_to(h(name), options, :class => name), :class => "current" 
    else 
    content_tag :li, link_to(h(name), options, :class => name) 
    end 
end 
+0

感謝を。これは、リスト項目が「現在」でないときにクラス名を追加するだけです。私はアンカーにクラスを追加する必要があります "現在の" – busyPixels

+0

コメントありがとう、私は私の答えを更新しました。 – Brian

+0

私は、コードを理解していないと思うので、もう一度やり直してください。完璧に動作します...ありがとうございました! – busyPixels

関連する問題