简体   繁体   中英

Links in navbar with Rails and Bootstrap

Say I have a 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? Which doesn't look like a good options, since in this case I need to duplicate bootstrap 's styles. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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