문제

I have an elementary problem. I'm sure you can tell by the question that I am somewhat new to ruby and rails.

I have a navigation that I am trying to apply the "active" class to so I can style the active page differently. I have @cur_page set like this with a before_filter:

def cur_page
    @cur_page = params['action']
end

I am trying to simply output the text "active", but its not working.

<li class="button <%= "active" if @cur_page == "contact" %>"><%= link_to "Contact", :public_contact %></li>

I've also tried:

<li class="button <%= puts "active" if @cur_page == "contact" %>"><%= link_to "Contact", :public_contact %></li>

I've checked the instance variable to make sure that it is set, and it is displaying "contact" like I expect.

Any ideas?

도움이 되었습니까?

해결책

What is the :public_contact? I would recommend you to use current_page? helper instead of @cur_page.

<li class="button <%= "active" if current_page?(public_contact_path(@contact) %>"><%= link_to "Contact", public_contact_path(@contact) %></li>

Replace public_contact_path(@contact) to valid route helper.

다른 팁

I think the problem is with double quotes.

use 'active' instead of "active" use 'contact' instead of "contact"

<li class="button <%= 'active' if @cur_page == 'contact' %>"><%= link_to "Contact", :public_contact %></li>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top