Issues with Bootstrap 5 JavaScript Features in Rails 7 Application

I recently set up my first Rails 7 app and successfully installed Bootstrap 5 with no CSS errors. However, I am facing an issue where the javascript functions like dropdown menus and offcanvas are not working as expected.

To test, I used the following code:

<script>
  $( document ).ready(function() {
    console.log( "document ready!" );
    $( "#TEST" ).click(function() {
      console.log( "clicked!" );
    });
  });
</script>

Both console.log statements provide the right output, indicating that jQuery and js are functioning correctly. The problem seems to lie with Bootstrap.

Below is a snippet from my gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.1.0'

# RAILS GENERATED STUFF
gem 'rails', '~> 7.0.1'
...

In addition, here's the content of my application.js, located in 'javascript/controllers/application.js' but also present in `assets/builds/application.js`. Instead of webpack, I'm using rollup installed with

$ ./bin/rails javascript:install:rollup`:

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "controllers"
import 'offcanvas';

//= require jquery3
//= require jquery_ujs
//= require jquery-ui
//= require popper
//= require bootstrap
//= require activestorage
//= require font_awesome5
//= require tinymce



window.jQuery = $;
window.$ = $;

If anyone can identify where the issue might be, I'd greatly appreciate it!

Answer №1

I encountered a similar issue.

To resolve it, I included the Bootstrap bundle script within the body tags of my application.html.erb file like this:

<body>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b69646e7562707477660f70655d6c69751d75787b">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <%= yield %>
</body>

Answer №2

Ensure you have the correct version of Bootstrap when working with the provided example Bootstrap code. It's important to note that Version 4 markup is not compatible with Version 5 Bootstrap.

Specifically, this attribute has changed from:

data: { toggle: "dropdown"}

to

data: { "bs-toggle" => "dropdown"}

If your "Search" form appears stacked and isn't utilizing the full width, it may indicate using the incorrect version.

https://i.sstatic.net/vl2Gr.png

The expected appearance should resemble:

https://i.sstatic.net/lnDi3.png

This relates to the NavBar in version 5.3: https://getbootstrap.com/docs/5.3/components/navbar/

Answer №3

I encountered some significant issues with this. To resolve, navigate to app/javascript/application.js and make the following changes:

import "@hotwired/turbo-rails"

Replace it with:

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

Additionally, always use bin/dev to initiate the rails server in development, rather than using the rails server command.

For additional details

  • Refer to this link for more information

Answer №4

Within the body tag in application.html.erb, include the following script:

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f7fafae1e6e1e7f4e5d5a0bba6bba6">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

Following this, add the JS expression within the javascript/application.js:

myModal.addEventListener('shown.bs.modal', () => {
myInput.focus()
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Invoking PHP code from within Javascript will output the function as a direct string

I seem to be going in circles and missing something silly... My setup involves using CodeIgniter on the server-side and Bootstrap on the client, but that's not really the issue here... I am attempting to access a PHP value within a JavaScript functi ...

Eliminate disparity in Woocommerce shopping cart

I have a pizza with various toppings. When the user selects "Add Toppings," they appear as drop-down options defaulted to "none." I want to hide the selected "none" options in the cart and checkout. Although I've successfully hidden them on the cart ...

Utilize the input field value in an HTML dialog box to assign it to a variable in the code.gs file with the help

I'm trying to create a dialog box where the user can select a year, and then have the server process the selected value using the function doSomethingWithCompetitionYear(theYear). I've researched several discussions, but I'm having trouble ...

One way to invoke JavaScript within an AJAX response is to use it to close a form div after successfully submitting the form

I've set up a form that, upon submission, sends its data to another PHP script for validation using AJAX. Validation errors are displayed in a div within the form, while a success message is shown if the validation passes. However, I encountered an i ...

Accessing arrays using bracket notation

When working with TypeScript (or JavaScript), you have the option to access object properties using either dot notation or bracket notation: object.property object['property'] Let's explore a scenario using the latter method: const user = ...

Update the class of the element that is currently selected using jQuery and the `this` keyword

Is there a way to change the class on hover only for the current element using 'this'? The code I have changes classes for all elements, but I need it to work individually. Here is the code snippet I'm currently using: https://codepen.io/ky ...

Using JQuery's .mouseover and .mouseout methods to modify font colors on a webpage

Hi there, I'm new to JQuery and trying to experiment with some basic functionalities. I have a simple navigation menu created using an unordered list, and I want to change the font color of the currently hovered list item using JQuery. However, I&apos ...

Utilize module.exports to export objects containing callback functions

I am currently diving into the world of writing my own modules for nodejs. My goal is to create various objects that I can use throughout my application. Here is the result I am aiming for: //assuming we have a database: Person_table(ID int A_I, NAME var ...

Having difficulty incorporating custom JavaScript files into a testing framework

I am facing a unique challenge while integrating the Page Object design pattern into my test suite using selenium-webdriver and node.js. The first page object, pageObject/admin/login/index.js, works seamlessly. It contains selectors and methods to fill ou ...

Is it possible to display data on a webpage without using dynamic content, or do I need to rely on JavaScript

Imagine a scenario where I have a single-page website and I want to optimize the loading time by only displaying content below the fold when the user clicks on a link to access it. However, I don't want users with disabled JavaScript to miss out on th ...

The value of ng-model is consistently stored as a string, regardless of whether the input is a number or

<div> <input type="text" ng-model="test"/> </div> When a value is entered into the input field with the ng-model "test", it is always treated as a String type, even if it is a valid number. However, I am looking for a way to determine th ...

Show Particular Outcome at the Beginning of Array in ReactJS

I am working with an array of data that contains a list of names, and I need to sort it so that a specific name ("Fav Team") always appears at the top of the list. The array has two fields, homeTeamName and awayTeamName, that may contain this value. How ...

Embracing the imagery: How Bootstrap's media object wraps content around images

I am attempting to utilize Bootstrap's media object, but it doesn't seem to function as expected for me. Whenever I attempt to use the example code provided by Bootstrap (or any other tutorials like W3Schools), the content in the media body is al ...

Utilizing JavaScript for form creation

Still learning the ropes of JavaScript and feeling a bit unsure about my skills. I'm trying to use JavaScript to create a new window with an input form within it. Managed to get a basic window set up with a dropdown menu, but struggling to implement ...

How should I integrate my JS authentication function in Rshiny to enable the app to utilize the outcome?

Currently, I have an Rshiny application set to be published on the server but in order to ensure that a specific user has access, we require an API authentication token. The process of authentication is handled within JS tags outside of the application, wh ...

Working with Node.js and JavaScript's Date object to retrieve the time prior to a certain number of hours

I am currently working on a script in node.js that is able to locate all files within a specific directory and retrieves their modified time: fs.stat(path, function(err, states){ console.log(states.mtime) }) After running the script, it ...

What is the process for utilizing GruntFile.coffee and package.json to extract or create the Lungo.js example files?

I want to experiment with the Lungo.js examples found here: https://github.com/tapquo/Lungo.js. However, when I try to run the index.html in the example directory, it seems like the components and package directories are empty. Although these directories d ...

What is the best way to retrieve both the checked and unchecked values from a collection of checkboxes?

Check Out This live Example: I am in the process of creating a list of checkboxes with various data objects: data = [ { Key: "class_id", displayName: "Section ID", enabled: true }, { Key: "room_l4", displayName: "Location", enabled: false }, { Key: "se ...

What is the best way for me to automatically square a number by simply clicking a button?

How do I create a functionality that multiplies a number by itself when a specific button is clicked? I want this operation to only occur when the equals button is pressed. I have been attempting to set this up for over an hour without success. My current ...

Protractor - Easily locating elements by targeting their children

I am attempting to modify the text of the input element, but I am having trouble identifying it. The element does not have an id or any other distinguishing characteristic. Additionally, this same object is repeated multiple times in the code, making it di ...