Add an 'active' class to all active links in rails?

后端 未结 7 579
醉酒成梦
醉酒成梦 2021-02-05 15:08

Basically, I have a lot of code that looks like this:

link_to t(\'.profile\'), business_path(@business), class: \'#{\'active\' if current_page? business_path(@bu         


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 15:52

    I did the same that @egyamado. I needed to use AwesomeIcons too, so:

    A helper:

    def active_class?(link_path)
        'active' if current_page?(link_path)
    end
    

    And it was my view:

     <%= link_to my_controller_page_path,
        :title => "My Controller Page",
        :class => "other_name_class #{active_class?(my_controller_page_path)}" do  %>
                     My Controller Page
    <%end%>
    

    In another kind of Link, for example inside a Li.

    #In this case I put a extra validation in root_path
    
     
    

    It worked for me.

提交回复
热议问题