【问题标题】:"Uncaught TypeError: $(...).popover is not a function" RoR - Bootstrap - jQuery“未捕获的 TypeError:$(...).popover 不是函数” RoR - Bootstrap - jQuery
【发布时间】:2017-06-19 08:24:36
【问题描述】:

我已经查看了这个question on SO,因为我在 Rails 上遇到了类似的问题。 我正在尝试使 AJAX 请求工作(查找一些金融代码),我认为我的代码没问题(至少我希望如此),但我仍然收到这个奇怪的错误:

bootstrap.self-fdc98dee79ee88255e10cac6caa91338165cb76cf0d263744d8d90011fc2ef8f.js:3 Uncaught TypeError: $(...).popover is not a function 在 HTML 文档。 (bootstrap.self-fdc98dee79ee88255e10cac6caa91338165cb76cf0d263744d8d90011fc2ef8f.js:3) 着火了(jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:3233) 在 Object.fireWith [as resolveWith] (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:3363) 在 Function.ready (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:3583) 在 HTMLDocument.completed (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:3618)

这是我的脚本:

var init_stock_lookup;

init_stock_lookup = function() {
  $('#stock-lookup-form').on('ajax:success', function(event, data, status) {
    $('#stock-lookup').replaceWith(data);
    init_stock_lookup();
  })
}

$(document).ready(function() {
  init_stock_lookup();
})

这是页面:

<div id="stock-lookup">
  <h3>Search for stocks</h3>
  <%= form_tag search_stocks_path, remote: true, method: :get, id: 'stock-lookup-form' do %>
    <div class="form-group row no-padding text-center col-md-12">
      <div class="col-md-10">
        <%= text_field_tag :stock, params[:stock], placeholder: "Stock ticker symbol", autofocus: true, class: 'form-control search-box input-lg' %>
      </div>
      <div class="col-md-2">
        <%= button_tag(type: :submit, class: "btn btn-lg btn-success") do %>
          <i class="glyphicon glyphicon-search"></i> Look up a stock
        <% end %>
      </div>
    </div>
  <% end %>
  <% if @stock %>
    <div id='stock-lookup-results' class="well results-block">
      <strong>Symbol:</strong> <%= @stock.ticker %>
      <strong>Name:</strong> <%= @stock.name %>
      <strong>Price:</strong> <%= @stock.price %>
    </div>
  <% end %>

gem 文件:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.1'
gem 'devise'
gem 'twitter-bootstrap-rails'
gem 'devise-bootstrap-views'
gem 'sprockets-rails', :require => 'sprockets/railtie'
gem 'jquery-rails'
gem 'jquery-turbolinks', '~> 2.1'
gem 'stock_quote'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# 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 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# 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', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  gem 'sqlite3'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= 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

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

以及指向我的 gitHub 存储库的链接:https://github.com/Ardzii/finance-tracker

我正在使用 RoR 和 Bootstrap (twitter)。 我从之前提供的 SO 链接中了解到,我遇到了 jQuery 冲突,但我不知道在哪里,最重要的是,如何解决它......

一如既往地感谢你们的帮助!

-------- 编辑。 ------------ 这是我的 application.js 供参考(我添加了 //require twitter/bootstrap 行,感谢 Hieu 的回答)。

// 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 rails-ujs
//= require twitter/bootstrap
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

【问题讨论】:

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


    【解决方案1】:

    您必须将//= require twitter/bootstrap 添加到您的application.js

    ...
    //= require rails-ujs
    //= require jquery
    //= require jquery_ujs
    //= require twitter/bootstrap
    //= require turbolinks
    //= require_tree .
    ...
    

    在这里:https://github.com/seyhunak/twitter-bootstrap-rails#using-javascript

    【讨论】:

    • 嘿嘿!非常感谢你的帮助!但是,您的解决方案并没有完全解决问题。现在我得到一个额外的错误:Uncaught ReferenceError: jQuery is not definedUncaught TypeError: $(...).popover is not a function 仍然存在。现在我可以通过删除我的 application.js 中的以下行来使后者消失://= require jquery //= require jquery_ujs 但随后我得到了其他错误(显然,jQuery 不再正确包含)。有什么想法吗?
    • 嗨@Ardzii,你在require jQuery之后添加了那行吗,应该是这样的://= require jquery_ujs //= require twitter/bootstrap
    • 您好!谢谢你是最棒的!尽管我似乎还有其他无关的渲染问题,但它似乎正在工作。我现在将处理它们:)。再次感谢!
    • 嘿嘿!我不想再打扰你了,但我似乎无法找出为什么我没有得到任何渲染......如果有机会,你有几分钟的空闲时间我会喜欢获取您对以下问题的意见:*.com/questions/44628771/…
    • 嗨@Ardzii,我回答了你的问题,请检查:)