Setting the class of HTML elements using Rails?

☆樱花仙子☆ 提交于 2021-02-08 08:41:30

问题


I have a Rails app with Twitter Bootstrap installed. Bootstrap encapsulates each navigation bar link in a HTML list element, like so:

<ul class="nav">
    <li><a href="#home">Home</a></li>
    <li><a href="#products">Products</a></li>
    <li><a href="#settings">Settings</a></li>
</ul>

When a link is the current page, Bootstrap allows you to highlight it by setting the list element's class (not the link itself) to "active", like so:

    <li class="active"><a href="#products">Products</a></li>

My question is: how do you set the list element's class to "active" (if it is the current page) programmatically using Rails?

I know how it can be done for a link. Example:

<%= link_to "Products", products_path, :class => "active" if current_page?(:controller => "products") %>

But I don't know how it can be done for the parent list element.


回答1:


Use the content_tag helper:

<%= content_tag :li, :class => 'active' do %>
  List item contents
<% end %>


来源:https://stackoverflow.com/questions/11572514/setting-the-class-of-html-elements-using-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!