D
D
Dmitry2016-12-19 01:52:45
Ruby on Rails
Dmitry, 2016-12-19 01:52:45

How to properly highlight the current menu item in the site navigation?

Are there generally accepted techniques/gems for highlighting the current menu item?
Something looks ugly to me ... It turns out garbage:

%ul.side-nav
  - curclass = controller_name=="accounts" ? "sub-active" : ""
  - curclass = "active" if controller_name=="groups" || controller_name=="seasons"
  %li{class: curclass}
    = link_to admin_accounts_path do
      Люди
      - if controller_name=="accounts"
        %i.material-icons label_outline
    %ul
      - subcurclass = "sub-active" if controller_name=="groups"
      %li.child{class: subcurclass}
        = link_to admin_groups_path do
          Группы
          - if controller_name=="groups"
            %i.material-icons label_outline
    %ul
      - subcurclass = controller_name=="seasons" ? 'sub-active' : ''
      %li.child{class: subcurclass}
        = link_to admin_seasons_path do
          Сезоны
          - if controller_name=="seasons"
            %i.material-icons label_outline

Criticize, please, and tell me how practical and beautiful!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2016-12-19
@svetozar

Just create a helper. Example:

def nav_link(link_text, link_path)
  class_name = current_page?(link_path) ? 'current' : ''

  content_tag(:li, :class => class_name) do
    link_to link_text, link_path
  end
end

nav_link 'Home', root_path

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question