简体   繁体   English

导航栏中带有Rails和Bootstrap的链接

[英]Links in navbar with Rails and Bootstrap

Say I have a navbar : 假设我有一个navbar

<ul class="nav navbar-nav">
  <li><%= link_to 'Item 1', item1_path %>
  <li><%= link_to 'Item 2', item2_path %>
  ...

But this way current menu item is also a link. 但是这样,当前菜单项也是一个链接。 How do I make it into a span , preserving appearance? 如何使它成span ,并保持外观? Which doesn't look like a good options, since in this case I need to duplicate bootstrap 's styles. 这似乎不是一个好的选择,因为在这种情况下,我需要复制bootstrap的样式。 Or make it look like a menu item without it pointing to any page? 还是使其看起来像菜单项而没有指向任何页面?

A link_to method will always render an <a> tag, but you can render a <span> inside of the <a> tag if you use link_to ... do link_to方法将始终呈现<a>标记,但是如果您使用link_to ... do则可以在<a>标记呈现<span> link_to ... do

This is an example taken from this documentation 这是文档中的示例

<%= link_to(@profile) do %>
  <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
<!-- which renders -->
<a href="/profiles/1">
       <strong>David</strong> -- <span>Check it out!</span>
</a>

I came up with the following helper: 我想出了以下助手:

module ApplicationHelper
  def menu_item(name, path)
    if current_page? path
      content_tag('a', name)
    else
      link_to name, path
    end
  end
end

With that we've just got to replace link_to with menu_item in original code. 这样,我们只需要用原始代码中的menu_item替换link_to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM