Best way to highlight current page in Rails 3? — apply a css class to links conditionally

后端 未结 8 1732
野趣味
野趣味 2020-12-04 14:25

For the following code:

<%= link_to \"Some Page\", some_path %>

How do I apply a css class current using the

8条回答
  •  遥遥无期
    2020-12-04 14:46

    In my case I have a lot of name spaced controllers, that is why I like to show if the current view also is in the Menu Path, I had use the solution of Michael van Rooijen and then I customize for my case.

    Helper

    def cp(path)
      "current" if request.url.include?(path)
    end
    

    View

    <%= link_to "All Posts", posts_path, class: cp(posts_path) %>
    

    Now if my menu bar is /users and my current page is /users/10/post also the link /users is set with "current" class

提交回复
热议问题