0
votes

I'm a beginner of rails programming. This is my first question in sof. I want to give my nav var "active" class when current page is included in the array of links.

I know how to check 1:1 in application_helper.rb

def is_active?(path)
   current_page?(path)? "active" : ""
end

As far as I know, current_page?(x) method only show true/false.

How to check in array?

for example:

<li class="<%= is_active?([a_1_path,a_2_path,a_3_path]) %>">

I tried this:

def is_active?(paths)
    y=0
    for x in paths
        current_page?(x)? (y=1): ""
    end
    y=1? "active":""
end  

but this makes all the menus in nav var active.

Do I have to render other pages in one page to control and keep active class in nav var among multiple pages?

2
is_active?([a_1_path,a_2_path,a_3_path], you are sending more than one paths so in is_active? you need to loop through each path to check.Anand
and not sure what you want in header, it can be done on another way also.Anand

2 Answers

0
votes
def is_active?(paths)
  paths.include?(request.path) ? "active" : ""
end
0
votes

Try these

<li class="<%= [a_1_path,a_2_path,a_3_path].include?(#current_page_path) ? 'active' : '' %>">