Rails Uncaught TypeError:$(...)。modal不是一个函数

时间:2019-06-24 15:47:00

标签: jquery ruby-on-rails bootstrap-modal

我试图在视图中使用模式,我从https://getbootstrap.com/docs/4.1/components/modal/复制了代码以设置示例模式以供使用,但是按钮不会激活模式。

当我尝试使用$('#myModal').modal('toggle');在控制台中强制执行该操作时,出现Uncaught TypeError: $(...).modal is not a function错误。我看过其他问题,答案通常是缺少jQuery或首先需要使用jQuery的问题,但是我已经确定我需要jQuery,并且它在引导之前就存在。

我的Gemfile:     红宝石的'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');时出现,但不会出现。

1 个答案:

答案 0 :(得分:0)

好像您在application.js文件中的require之前错过了bootstrap关键字