I am trying to use a basic current_page method to define whether a navigation link should be highlighted as the current page or not.
I am not receiving any errors at the moment but am clearly not defining things properly as it’s not using my CSS correctly.
In my PagesController I have the following:
def current_page
current_page = (path)
end
and I am using an if and else statement on my pages view to try and define which CSS line to use which looks like this:
<div id="nav">
<ul>
<li>
<% if "current_page" %>
<a href="/about" class="about-cp">About</a>
<% else %>
<a href="/about" class="about">About</a>
<% end %>
</li>
</ul>
</div>
I have read quite a few forums on this but still can’t seem to get it right.
Mhh you got an error in your code. With
if "current_page"you will get always true, and only the first link will get rendered. You should useif current_pageinstead.But rails has a build in helper, called
current_page?for exactly this purpose:Just do it like this:
See here for more information on
current_page?