javascript - Bootstrap 隐藏模式在使用 ajax 的 Rails 6 中不起作用
问题描述
我试图通过 jquery 调用在 rails 6 中隐藏引导模式,但经过 2 周的尝试后,我似乎无法让它工作......
我的模态设置在这样的视图中:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#logModal">
Launch demo modal
</button>
<!-- Modal -->
<%= simple_form_for(@log, remote: true) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<div class="modal fade" id="logModal" tabindex="-1" role="dialog" aria-labelledby="logModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="logModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-inputs">
<%= f.association :user %>
<%= f.association :project %>
<%= f.input :body %>
</div>
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="logmodalclose">Close</button>
<%= submit_tag "create", close: "btn btn-primary" %>
</div>
</div>
</div>
</div>
<% end %>
然后控制器已format.js
添加到 create 方法。
然后,我为视图创建了一个文件 Create.js.erb,其中包含以下代码
$('#logModal').modal('hide');
当我查看网页时,模式会加载但不会关闭。如果我在隐藏模式行之前发出警报,则会出现警报,因此路由显然正在加载。如果我粘贴$('#logModal').modal('hide');
到 chrome 浏览器控制台中,我会收到以下错误
Uncaught TypeError: $(...).modal is not a function
查看其他答案,这似乎是由于两次加载 jquery 或 jquery 和 bootstrap 以错误的顺序引起的。我花了一个星期尝试不同的东西,但似乎没有任何效果。我正在使用 rails 6 和 app/javascript/paths 中的 appication.js 文件读取:
require("jquery")
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("bootstrap/dist/js/bootstrap")
// Tomas added to import Style sheets
import '../stylesheets/application'
import './bootstrap_custom.js'
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)
//= require jquery3
//= require popper
//= require bootstrap-sprockets
老实说,我不明白Require("Jquery")
和//= require jquery
我对 Rails 相当陌生......
非常感谢任何帮助
更新:当我在视图底部添加以下内容时它可以工作
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
但是,我认为这些库应该通过 Application.js 加载......我想如果我能让它工作,这将是一个更好的方法......?
解决方案
这可能是一个彻底的黑客,但我能够通过在 application.js 中明确地将 jquery 设置为全局对象来解决这个问题。
import jquery from 'jquery';
window.$ = window.jquery = jquery;
应用程序.js:
require("@rails/ujs").start();
require("@rails/activestorage").start();
require("channels");
import jquery from 'jquery';
window.$ = window.jquery = jquery;
import "bootstrap";
import "../stylesheets/application";
环境.js
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
Popper: ['popper.js', 'default']
})
)
module.exports = environment
推荐阅读
- c# - 如何从特定格式的xml中读取数据
- python - 如何将“”添加到名称列表中?
- vaadin - 可切换 Vaadin 12 主题的最佳实践
- javascript - 引用变量与“实际”变量(PHP 与 Javascript)
- api - 如何将 tomtom 映射包装到引导容器中
- typescript - npm 脚本未正确启动应用程序
- amazon-web-services - 我可以使用现有的 AWS IAM 角色通过 Cloudformation 模板创建 S3 存储桶吗?
- html - 日期选择器在 Shopify 的页面上不起作用
- python - Keras flow_from_dataframe 提供 0 张图片
- firebase - 如何从 firebase_auth 测试“signInWithEmailAndPassword”