1

In my view, I have:

<li class="<%= 'active' if current_page?(:controller => 'posts') %>"><a href="/posts">Posts</a></li>

The problem is, when I move away from /posts/ path, the class is turned off again. For example, when I go to /posts/1, the link class is not active anymore. I thought it would still be active because the controller stays the same. How should I fix the problem?

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Maximus S
  • 10,759
  • 19
  • 75
  • 154

2 Answers2

1

You can do

<%= 'active' if params[:controller] == 'posts' %> 

so try to debug what's the value of

params[:controller] 

when you go to /posts/1 to see why class "active" doesn't appear.

It could be that the class "active" is there but its not showing for a css issue? Check the source code on the browser on /posts/1* to see if the code is working or not

rorra
  • 9,593
  • 3
  • 39
  • 61
  • params[:controller] was consistently 'posts', but class active was not turned on when I used current_page? method. Weird. – Maximus S Dec 07 '12 at 08:22
1
<li class="<%=params[:controller] == 'posts' ?  'active' : '') %>">
    <a href="/posts">Posts</a>
 </li>

The above will work for sure.

Gowri Naidu R
  • 1,964
  • 2
  • 13
  • 15