• Best way to create simple menu in rails app

    I have a simple menu that looks like this:

    <ul class="menu">
         <li class="active"><a href="<%= root_path %>">Home</a></li>
         <li class=""><%= link_to 'Feeds' , feeds_path %></li>
         <li class=""><%= link_to 'Accounts' , accounts_path %></li>
    </ul>
    
          

    The "active" class is a style to mark my current page.

    I have two questions:
    1. How can I tell "application" which page I am on and ask to "change" the class to active?
    2. Is there a better way to create this menu (possibly controller driven or db table driven)?

    I realize this is a newbie question, but I've been thinking about this for a few days, read a few tutorials, but none of them really clicked.

    Thank you for your help.

    +3


    source to share


    4 answers


    I am using a method current_page?

    to set an active link. It takes a path as a parameter. I create a hash of the link and path texts and iterate over it by printing the links. So I only need to call current_page? once.



    There are gems that can help you. Take a look at them: https://www.ruby-toolbox.com/categories/rails_menu_builders

    0


    source


    I did this recently in ApplicationHelper

    :

      def nav_links
        items = [home_link, about_me_link, contact_link]
        content_tag :ul, :class => "nav" do
          items.collect { |item| concat item}
        end
      end
    
      def home_link
        nav_item_active_if(!@article || @article.type.nil?) do
         link_to "Home", root_path
        end
      end
    
      def about_me_link
        nav_item_active_if(@article && @article.type == "About") do
          link_to "About Me", article_path(About.first)
        end
      end
    
      def contact_link
        nav_item_active_if(@article && @article.type == "Contact") do
          link_to "Contact", article_path(Contact.first)
        end
      end
    
      def nav_item_active_if(condition, attributes = {}, &block)
        if condition
          attributes["class"] = "active"
        end
        content_tag(:li, attributes, &block)
      end
    
          

    In your view, you can simply call:



          <%= nav_links %>
    
          

    Perhaps you can use it as an example.

    +1


    source


    Not a perfect solution, but you can do the following.

    create 3 variables:

    @homeActive = ""
    @feedsActive = ""
    @accountsActive = ""
    
          

    In the code you provided, set a class for each variable that matches a name.

    <li class=@homeActive><a href="<%= root_path %>">Home</a></li>
    
          

    Now in your controller under the home method let's say set @homeActive = "active"

    and the other two to "". Repeat this for other methods and it should work.

    0


    source


    I used a bunch of 'if and current_page? method. It's ugly, but it worked, if anyone has a better idea on how to do this, I'd love to know about it.

    if current_page? (root_path)
       content_tag(:li , link_to('Home' , root_path), :class => "active")
     else
       content_tag(:li , link_to('Home' , root_path))
    end
    
          

    0


    source







    All Articles