css - Rails - 类不适用于链接到 Controller /操作

标签 css ruby-on-rails

我需要为子菜单的事件菜单项加下划线。我发现我可以在我的菜单中使用以下内容。我可以通过申请类(class)来做到这一点:

<li><%= link_to "* RFQ", rfq_path(1), class: get_rfqs_class() %></li>

并且有一个辅助元素

  def get_rfqs_class
    if controller_name == 'rfqs'
      return 'active'
    end
  end

我为每个元素都有一个 get_xxx_class() 和一个相应的助手。

效果很好。但是,我需要为另一个菜单项做一些不同的 link_to

    <li> <%= link_to "Pre Production Meeting",  controller: 
'pre_production_meetings', action: 'show', id: 1 , 
op_id: $current_opportunity, class: get_pre_production_meetings_class() %> </li>

问题是不同的 link_to 格式导致下划线不显示。如果我进行检查,它会显示类(class)处于事件状态,但我看不到下划线

<a href="/pre_production_meetings/1?class=active">Pre Production Meeting</a>

我尝试在以下代码中使用 div 和 span,但没有成功。

   <% if controller_name == 'pre_production_meetings' then %>
                <li> <%= link_to "Pre Production Meeting",  controller: 'pre_production_meetings', action: 'show', id: 1 , op_id: $current_opportunity, class: get_pre_production_meetings_class() %> </li>
          <% else %>
                <li> <%= link_to "Pre Production Meeting", controller: 'pre_production_meetings', action: 'show', id: 1 , op_id: $current_opportunity %></li>

          <% end %>

像我那样使用 Controller / Action 的 link_to 是否会阻止类被正确应用?

此外,我还检查了按预期工作的菜单项的样式

ul.art-hmenu>li>a.active {
    padding: 0 12px;
    margin: 0 auto;
    color: #145366;
    text-decoration: underline;
}

如果我检查生产前 session 样式,我看不到 a.active 但看到 a.hover

ul.art-hmenu>li>a:hover, .desktop ul.art-hmenu>li:hover>a {
    color: #000000;
    text-decoration: none;
}

如果我将文本装饰更改为下划线,那么我会按预期看到下划线。

最佳答案

link_to 的那个实例使用这种格式:

link_to(body, url_options = {}, html_options = {})

您需要将您的 url 选项括在花括号中,这样链接看起来像这样:

<a href="/pre_production_meetings/1" class="active">

您的类(class)已嵌入到 url 而不是 html 中。

这应该可以解决您的问题:

<%= link_to "Pre Production Meeting",
            {controller: 'pre_production_meetings', action: 'show', id: 1 ,
              op_id: @current_opportunity},
            class: get_pre_production_meetings_class() %>

此外,$ 可能是错字。我假设它是一个@。

关于css - Rails - 类不适用于链接到 Controller /操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667295/

相关文章:

ruby-on-rails - Rails 中的弱 ETAG?

html - Firefox:将鼠标悬停在带有下拉菜单的按钮上会立即隐藏下拉菜单

css - Windows 中 Outlook 电子邮件中的响应式填充

html - 如何在当前模型部分添加下拉 YearID

ruby-on-rails - Dropzonejs 和 Paperclip、url 和 paramName?

ruby-on-rails - 如何通过设置关键元素的优先级来使用字符串键对 ruby​​ 散列进行排序?

html - 在 2 列文本之间 float 图像

css - 在宽度为 :100% IE7 的 div 容器中向左浮动和向右浮动

ruby-on-rails - 将 Rails has_one 关系修改为 UPDATE 而不是 DELETE 然后 INSERT

javascript - Event_calendar gem 没有方法 'observe'