L
L
lavezzi12015-08-08 20:34:21
Ruby on Rails
lavezzi1, 2015-08-08 20:34:21

How to set a class for certain pages?

Hello. There is such code:
In application.html.haml

- if user_signed_in?
      %nav.navigation
        %ul
          %li
            = link_to "New project", new_place_path
          %li
            = link_to "Profile"
          %li
            = link_to "Sign Out", destroy_user_session_path, method: :delete

    - else
      %nav.navigation_welcome
        .inner
          %ul
            %li
              = link_to "Login", new_user_session_path
            %li
              = link_to "Sign up", new_user_registration_path

On the main page, the background of the menu on top is transparent, and when you go to pages like /login & /sign_up, you need to change the color of the menu. What is the best way to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TyzhSysAdmin, 2015-08-08
@POS_troi

$(function () {
    var url = window.location;
    $('#top-menu a[href="' + url + '"]').parent('li').addClass('current-page');
    $('#top-menu a').filter(function () {
        return this.href == url;
    }).parent('li').addClass('current-page').parent('ul').slideDown().parent().addClass('active');
});

<div id="top-menu">
 <ul class="nav child_menu">
  <li><a href="index.html">Ololo</a></li>
  <li><a href="index2.html">Ololo2</a></li>
 </ul>
</div>

Adds the current-page class to the menu item whose href matches the current URL.
<li class="current-page"><a href="index2.html">Ololo2</a></li>

P
Pavel Kononenko, 2015-08-08
@premas

You can write your own helper, you can make before_action in your HomeController, which will set the variable.
maybe on the forehead
You can bind not to the controller, but to request.url
The best way is to use angular)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question