1

I am trying to highlight curent link. I used this question for ideas here
I have simple menu, that is based on this structure:

<li>
   <%= @waste_root.name %>
   <ul>
      <% @wast_child_ids.each do |it| %>
      <li><%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path)} additional_class" %>   
      </li>
      <%end%>
   </ul>
</li>

In Aplication helper:

def cp(path)
  "current" if current_page?(path)
end

In CSS file:

.current {
color:red;

}

What I get is all links are in red color. I don't get it. For others it worked just fine.

Community
  • 1
  • 1
Edgars
  • 913
  • 1
  • 19
  • 53

2 Answers2

2

You need to pass the category.

<%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path(:category =>  it.name))} additional_class" %> 

They're all on the products path because you're just differentiating based on parameters. So just passing the products path they all return true, you need to differentiate based on the parameters by passing them too as you've done in your link

j-dexx
  • 10,286
  • 3
  • 23
  • 36
2

I think your path in view should look like this:

<li>
   <%= @waste_root.name %>
   <ul>
      <% @wast_child_ids.each do |it| %>
      <li><%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path(:category =>  it.name))} additional_class" %>   
      </li>
      <%end%>
   </ul>
</li>

Because, you're passing a param category in products_path and hence current_page? isn't able to judge the correct relative path. Also, it'd be better if you use the _url(complete path) instead of relative path. It'll be much clear to cross check and to understand as well.

Surya
  • 15,703
  • 3
  • 51
  • 74