I have been Trying to figure this out for some time now. I have a navigation where the list item gets a class of "active" when on that page. I need to also give the anchor of those list items each a unique class. So my code is:
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
Navigation
<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>
This Generates
<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>
I need it to generate
<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>
I have tried this many ways but everything i try just breaks it. Any help on this issue would be much appreceatied.
This looks like it might work:
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