Rails Uncaught TypeError: $(...).modal 不是函数
Posted
技术标签:
【中文标题】Rails Uncaught TypeError: $(...).modal 不是函数【英文标题】:Rails Uncaught TypeError: $(...).modal is not a function 【发布时间】:2019-11-06 11:09:51 【问题描述】:我正在尝试在视图中使用模态,我从 https://getbootstrap.com/docs/4.1/components/modal/ 复制了代码来设置一个示例模态以使用,但按钮不会激活模态。
当我尝试在控制台中使用$('#myModal').modal('toggle');
强制它时,我收到Uncaught TypeError: $(...).modal is not a function
错误。我查看了其他问题,答案通常是缺少 jQuery 或需要首先需要 jQuery,但我确保我需要 jQuery 并且它在引导之前。
我的宝石文件: 红宝石'2.5.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for javascript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'popper_js', '~> 1.11.1'
gem 'bootstrap', '4.0.0.alpha6'
gem 'will_paginate', '> 3.0'
gem 'simple_form'
gem 'font-awesome-rails'
gem 'devise'
gem 'geocoder'
gem 'figaro'
source 'https://rails-assets.org' do
gem 'rails-assets-tether', '>= 1.3.3'
end
我的 application.js:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require popper
//= require tether
//= bootstrap-sprockets
//= require_tree .
我的视图模式: 模态标题 × ... 关闭 保存更改
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
我希望在单击按钮时出现模式。它没有。我希望当我运行 $('#myModal').modal('toggle');
时会出现模态,但它不会。
【问题讨论】:
安装gem后,别忘了重启你的rails服务器。 【参考方案1】:看来您在 application.js 文件中错过了 bootstrap
之前的 require
关键字
【讨论】:
就是这样!太感谢了!我看了三遍这个文件,不知道我是怎么错过的。或者其他引导样式如何在整个应用程序中应用。再次感谢!以上是关于Rails Uncaught TypeError: $(...).modal 不是函数的主要内容,如果未能解决你的问题,请参考以下文章
Uncaught TypeError: this.source is not a function when using rails4-autocomplete
Jquery-ui Datepicker 未显示在 Rails 视图中,出现错误 Uncaught TypeError: $(...).datepicker is not a function
Rails 6.1.3,webpacler,select2,出现错误:readyException.js:6 Uncaught TypeError: $(...).select2 is not a f
readyException.js:6 Uncaught TypeError:无法读取未定义的属性“mData”
Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerText') in OpenWetherMa
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'fingerprint') Laravel Live