admin管理员组文章数量:1405301
I'm using a Railsbytes script to install bootstrap 5 alpha; .
However, I can't get any of the tooltips and popovers to work. Not sure even where I should put the scripts or if I have to add anything to it in order to enable them. Would anyone please be so kind and provide some guidance. Thanks a lot!
/ Jacob
Example of js I suspect I need to add somewhere according to :
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
I'm using a Railsbytes script to install bootstrap 5 alpha; https://railsbytes./public/templates/VB0s5v.
However, I can't get any of the tooltips and popovers to work. Not sure even where I should put the scripts or if I have to add anything to it in order to enable them. Would anyone please be so kind and provide some guidance. Thanks a lot!
/ Jacob
Example of js I suspect I need to add somewhere according to https://v5.getbootstrap.:
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
Share
Improve this question
asked Nov 10, 2020 at 22:54
jacobrubyonrailsjacobrubyonrails
1472 silver badges10 bronze badges
2 Answers
Reset to default 8I struggled with this too but have got them working now. This is my workflow for getting Bootstrap5 working with Rails6.
yarn add bootstrap@next
yarn add @popperjs/core
- create file
app/javascript/stylesheets/application.scss
which is where I will add custom css - add the following to the top of the new file:
# app/javascript/stylesheets/application.scss
@import "bootstrap"
- In order for the application to import your custom css from the javascripts folder, add the following to your application layout file add
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
. The pack_tag indicates that this will be handled by webpacker. The layout file will then look like:
# app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
- finally to get everything working add the following to the bottom of app/javascript/packs/application.js
# app/javascript/packs/application.js
import * as bootstrap from 'bootstrap'
import "../stylesheets/application"
document.addEventListener("DOMContentLoaded", function(event) {
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
});
Just as a test I then added the following Bootsrap ponents to my home page (or any page to test):
# index.html.erb
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip on top">
Tooltip on top
</button>
Since version 5, Bootstrap bundle files already contain Popper, so there's NO need to add this package to your package.json
本文标签: javascriptBootstrap 5 tooltippopover and toasts not working in Ruby on Rails 6Stack Overflow
版权声明:本文标题:javascript - Bootstrap 5 tooltip, popover and toasts not working in Ruby on Rails 6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744253410a2597358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论