Why is my drop down menu acting strangely?

Socramvm

So I'm creating a rails application with a nav bar at the top of the page. However, when I implement the following code and click on "Categories" the names do not get displayed. Im also attaching a picture.

<ul class="nav navbar-nav navbar-right">

    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle= "dropdown" role="button"
      aria-expanded="false">Categories <span class="caret"></span></a>
      <ul class="dropdown-menu" role="menu">
        <% Category.all.each do |category| %>
        <li class="<%= 'active' if params[:category] == category.name%>">
          <%= link_to category.name, attractions_path(category: category.name), class: "link"%>
        </li>
        <% end %>
      </ul>
    </li>

menu does not drop down.picture

Now, when I change my code to the following, I can see the items that should be displayed in the drop down menu. What am I doing wrong? why is it behaving like this?

<ul class="nav navbar-nav navbar-right">

    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle= "dropdown" role="button"
      aria-expanded="false">Categories <span class="caret"></span></a>
      <ul class="dropdown-toggle" role="menu">
        <% Category.all.each do |category| %>
        <li class="<%= 'active' if params[:category] == category.name%>">
          <%= link_to category.name, attractions_path(category: category.name), class: "link"%>
        </li>
        <% end %>
      </ul>
    </li>

links are there.picture

So basically Im just changing this line <ul class="dropdown-toggle" role="menu">.

Why is my menu not dropping down displaying all my categories?

mu is too short

Looks like you're trying to use a Bootstrap3 navbar set up with Bootstrap4. There have been some changes but you should be able to rebuild it based on the example in the documentation:

  • The items in your nav should be marked with nav-item classes.

  • You don't need to add the caret (<span class="caret">) yourself anymore.

  • The items in your drop down need to be marked with dropdown-item classes now.

  • The dropdown toggle should look like:

    <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" id="someId">
    

    rather than:

    <a href="#" class="dropdown-toggle" data-toggle= "dropdown" role="button" aria-expanded="false">
    
  • The dropdown menu should be linked back to its toggle with aria-labelledby="someId".

Putting those together gives you something like this:

<ul class="nav navbar-nav navbar-right">
  <li class="nav-item dropdown">
    <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" id="someId">
      Categories
    </a>
    <ul class="dropdown-menu" role="menu" aria-labelledby="someId">
      <% Category.all.each do |category| %>
        <li class="dropdown-item <%= 'active' if params[:category] == category.name %>">
          <%= link_to category.name, attractions_path(category: category.name), class: "link"%>
        </li>
      <% end %>
    </ul>
  </li>
  ...
</ul>

You'd want to use something sensible to replace someId of course.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related