Semantic UI Dropdown not Functioning in Rails 7 Application

88 views Asked by At

I'm encountering an issue with a Semantic UI dropdown in my Rails 7 application that I'm developing using a CDN. Despite my efforts, the dropdown is not working as expected. Below is the relevant code:

application.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title>OnlineChattingAppInRails</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
    <%= javascript_importmap_tags %>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
  </head>

  <body>
    <%= render 'shared/header' %>
    <div class="ui container">
      <%= yield %>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
    <%= javascript_include_tag 'custom' %>
  </body>
</html>

_header.html.erb:

<div class="ui small inverted menu">
  <div class="ui container">
    <a class="item">Home</a>
    <a class="item">Messages</a>
    <div class="right menu">
      <div class="ui dropdown item">
        Language <i class="dropdown icon"></i>
        <div class="menu">
          <a class="item">English</a>
          <a class="item">Russian</a>
          <a class="item">Spanish</a>
        </div>
      </div>
      <div class="item">
        <div class="ui primary button">Sign Up</div>
      </div>
    </div>
  </div>
</div>

custom.js (app/javascript/custom.js):

$(document).on("turbolinks:load", function () {
  $(".ui.dropdown").dropdown();
});

Despite these configurations, the dropdown doesn't behave as expected. I've ensured that jQuery and Semantic UI are included correctly. Could you help me identify the issue and provide a solution?

1

There are 1 answers

0
Afaq Shahid Khan On

I just remove this line from application.html.erb:

<%= javascript_include_tag 'custom' %>

And replace it by the following code just before the closing of body tag:

<script>
  $(".ui.dropdown").dropdown();
</script>