【问题标题】:Setting the class of HTML elements using Rails?使用 Rails 设置 HTML 元素的类?
【发布时间】:2012-07-19 07:50:29
【问题描述】:

我有一个安装了 Twitter Bootstrap 的 Rails 应用程序。 Bootstrap 将每个导航栏链接封装在一个 HTML 列表元素中,如下所示:

<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>

当链接是当前页面时,Bootstrap 允许您通过将列表元素的类(而不是链接本身)设置为“活动”来突出显示它,如下所示:

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

我的问题是:如何使用 Rails 以编程方式将列表元素的类设置为“活动”(如果是当前页面)?

我知道如何为链接完成。示例:

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

但我不知道如何为父列表元素做到这一点。

【问题讨论】:

    标签: html css ruby-on-rails twitter-bootstrap navbar


    【解决方案1】:

    使用content_tag helper

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

    【讨论】:

    • 但是只有当它是当前页面时,如何将类设置为“活动”?我试过&lt;%= content_tag :li, :class =&gt; 'active' if current_page?(:controller =&gt; "products") do %&gt; 但这不起作用。
    • 啊,想通了。必须是:&lt;%= content_tag :li, :class =&gt; ('active' if current_page?(:controller =&gt; "products")) do %&gt;。换句话说,类的值需要封装在括号中。 @georgebrock 你能更新你的答案来反映这一点吗?
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多