[英]Bootstrap hide modal not working in Rails 6 using ajax
I am trying to hide a bootstrap modal in rails 6 via a jquery call but after 2 weeks of trying I can't seem to get it to work...我试图通过 jquery 调用在 rails 6 中隐藏引导模式,但经过 2 周的尝试后,我似乎无法让它工作......
My modal is setup in a view like this:我的模态是在这样的视图中设置的:
<!-- 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 %>
The controller then has format.js
added to the create method. controller 然后将
format.js
添加到创建方法中。
I have then created a file Create.js.erb to the views with the following code in it然后我为视图创建了一个文件 Create.js.erb,其中包含以下代码
$('#logModal').modal('hide');
When I view the webpage the modal loads but doesn't close.当我查看网页时,模式会加载但不会关闭。 If I put an alert before the hide modal line then the alert appears so the routing is obviously loading.
如果我在隐藏模式行之前放置一个警报,那么警报就会出现,所以路由显然正在加载。 If I paste
$('#logModal').modal('hide');
如果我粘贴
$('#logModal').modal('hide');
into the chrome browsers console I get the following error进入 chrome 浏览器控制台我收到以下错误
Uncaught TypeError: $(...).modal is not a function
looking at other answers this seems to be caused by loading jquery twice or jquery and bootstrap in the wrong order.查看其他答案,这似乎是由于加载 jquery 两次或 jquery 并以错误的顺序引导引起的。 I have spent a week trying different things and nothing seems to work.
我花了一个星期尝试不同的事情,但似乎没有任何效果。 I am using rails 6 and the appication.js file in app/javascript/paths reads:
我正在使用 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
To be honest I don't understand the difference between Require("Jquery")
and //= require jquery
I'm fairly new to Rails...老实说,我不明白
Require("Jquery")
和//= require jquery
之间的区别 我是 Rails 的新手...
any help much appreciated非常感谢任何帮助
UPDATE: It works when I add the following at the bottom of the view更新:当我在视图底部添加以下内容时它起作用
<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>
However, I think these libraries should be loading via the Application.js... I pressume if I could get it to work this would be a better way of doing it...?但是,我认为这些库应该通过 Application.js 加载......我想如果我能让它工作,这将是一个更好的方法......?
This might be a total hack but I was able to resolve this issue by setting jquery to the global object explicitly in application.js.这可能是一个彻底的黑客,但我能够通过在 application.js 中明确地将 jquery 设置为全局 object 来解决这个问题。
import jquery from 'jquery';
window.$ = window.jquery = jquery;
application.js:应用程序.js:
require("@rails/ujs").start();
require("@rails/activestorage").start();
require("channels");
import jquery from 'jquery';
window.$ = window.jquery = jquery;
import "bootstrap";
import "../stylesheets/application";
environment.js环境.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
Another option is to use expose-loader另一种选择是使用expose-loader
src/index.js源代码/index.js
import $ from "jquery";
webpack.config.js webpack.config.js
module: {
rules: [
{
test: require.resolve("jquery"),
loader: "expose-loader",
options: {
exposes: ["$", "jQuery"],
},
},
],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.