【问题标题】:Error: Cannot find module 'bootstrap-sprockets'错误:找不到模块“引导链轮”
【发布时间】:2020-05-08 13:22:25
【问题描述】:

虽然使用 rails 运行引导程序运行良好,但 JS 功能对我不起作用。例如,我不能折叠导航栏,但我可以在浏览器控制台中运行$()

我的 /app/javascript/packs/application.js 看起来像这样:

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require('bootstrap-sprockets')
require('popper.js')
require('turbolinks')

// As instructed in https://github.com/twbs/bootstrap-rubygem#a-ruby-on-rails 
//= require bootstrap-sprockets
// But the above is not working

我的 /app/assets/stylesheets/application.scss 看起来像这样:

@import "bootstrap" ;

body {
    margin: 0 ;
}

在我的 routes.rb 中,我有 root to: 'pages#home'。 home.html.erb 看起来像这样:

<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
  <main role="main" class="inner cover">
    <h1 class="cover-heading">Cover your page.</h1>
    <p class="lead">Cover is a one-page template for building simple and beautiful home pages. Download, edit the text, and add your own fullscreen background photo to make it your own.</p>
    <p class="lead">
      <a href="#" class="btn btn-lg btn-secondary">Learn more</a>
    </p>
  </main>

  <footer class="mastfoot mt-auto">
    <div class="inner">
      <p>Cover template for <a href="https://getbootstrap.com/">Bootstrap</a>, by <a href="https://twitter.com/mdo">@mdo</a>.</p>
    </div>
  </footer>
</div>

当我运行服务器并访问主页时,浏览器控制台上显示错误:

Error: Cannot find module 'bootstrap-sprockets'

我的 gem 文件有 jquery 和 bootstap 的这些条目:

gem 'jquery-rails', '~> 4.3', '>= 4.3.5'
gem "bootstrap", "~> 4.4"
gem 'bootstrap-sass'

我无法在引导程序中使用导航栏折叠等功能。怎么了?

【问题讨论】:

    标签: ruby-on-rails bootstrap-4


    【解决方案1】:

    我遇到了崩溃无法正常工作的问题,以及最新版本的 jQuery 和引导程序。 不得不改为: “引导程序”:“^4.4.1”, “jquery”:“3.4.1”

    但这是使用 rails 6.0.1,所以使用 ujs。检查那些版本。

    【讨论】:

    • 当您尝试折叠时浏览器上的任何 javascript 错误?
    • 不,但我已经用 webpacker 解决了这个问题。我很快就会写这篇文章!
    • @S.Goswami 那么你是如何解决这个问题的呢?
    • 我想我已经使用 yarn (webpacker) 来安装 bootstrap、jquery 和 popper.js:qr.ae/pNsjrn Gem bootstrap 不能像 Rails 6+ 的预期那样工作
    猜你喜欢
    • 2015-09-29
    • 1970-01-01
    • 2019-05-25
    • 2021-01-02
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多