2

I have a working Rails 7 app that uses esbuild as the JS bundler and have imported bootstrap.

I am trying to figure out how to a access any Bootstrap Javascript functionality 'outside' the main application.js file - i.e. let's say I wanted to programatically show a Bootstrap modal on a particular page like this:

var myModal = new bootstrap.Modal(document.getElementById('helpModal'), {});
myModal.modal('show');

however this doesn't work as 'bootstrap' is not known:

Uncaught ReferenceError: bootstrap is not defined

...even though the page links in application.js:

<%= javascript_include_tag "application", defer: true %>

Any suggestions on how to access 'bootstrap' in JS outside application.js itself ?

Thank you !

4 Answers 4

1

Assuming you created your rails app with:

rails new APP_NAME --css=bootstrap ...

Which creates your app with ESBUILD and leaves you with a app/javascripts/application.js file looking like this:

// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"

You now need to code in the modal to use the imported bootstrap.

Add the code you have in your question below the bootstrap import line:

// Modals
var myModal = new bootstrap.Modal(document.getElementById('helpModal'), {});
myModal.modal('show');

// Or to have it triggered using a button
document.getElementById('helpModalButton').addEventListener('click', event => {
  myModal.show();
});

This will turn on the event handlers

More can be found on the Bootstrap Docs page for each of the Components (the same applies to Modals and Popovers)

https://getbootstrap.com/docs/5.1/components/modal/#passing-options

Sign up to request clarification or add additional context in comments.

1 Comment

This of course works, but the question asks "how to access any Bootstrap Javascript functionality 'outside' the main application.js file". I have a similar problem: an "import library" after the "import bootstrap" where I get an "bootstrap is not defined" inside that imported library.
1

I got this to work by setting bootstrap on window in application.js

// app/javascript/application.js
import * as bootstrap from "bootstrap"
window.bootstrap = bootstrap;

// In the view
:javascript
  new window.bootstrap.Modal('#helpModal').show();

To close the modal on "Close" and "Submit" this worked well:

<button type="button" class="btn btn-secondary" 'data-bs-dismiss'="modal">Close</button>

<%= form.submit "Submit", class: "btn btn-primary", 'data-bs-dismiss': "modal" %>

Adding "data-bs-dismiss" to the submit button closes it on submit.

Comments

0

I'm not sure how to help fix that error in an imported library but to reference bootstrap from a JS file within my app (but outside application.js) I duplicated the import * as bootstrap from "bootstrap" command in my Javascript file...

//app/javascript/popovers.js

console.log('Loading popovers...')

import * as bootstrap from "bootstrap"

const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))

Then I imported that file from my main application.js:

//app/javascript/application.js

// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"
import "./popovers.js"

For me, I was trying to reference bootstrap so I could initialise Popovers.

Comments

0

I just removed Gemfile.lock in my project and updated the gems using the command gem update --system.finally insatll gems using bundler bundle install I hope that works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.