【问题标题】:How to get Bootstrap Javascript to work in Ruby on Rails 6如何让 Bootstrap Javascript 在 Ruby on Rails 6 中工作
【发布时间】:2020-06-13 06:08:58
【问题描述】:

首先,我意识到这个问题之前已经被问过多次,但是我看到的所有答案都对我不起作用。

我已经使用 bootstrap gem 将 bootstrap 添加到我的 Rails 应用程序中,或者至少我认为我有。 bootstrap 的 css 和样式都在那里并且可以正常工作,但是缺少 javascript 功能

我对 Rails 还很陌生,所以我可能错过了一些非常明显的东西,我在下面附上了我认为相关的文件。

我有以下文件,任何建议或帮助将不胜感激

宝石文件

source 'https://rubygems.org'

ruby '2.7.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# 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.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'bootstrap', '~> 4.4', '>= 4.4.1'
gem 'devise'

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 web drivers to run system tests with browsers
gem 'webdrivers'
end

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

app/assets/stylesheets/applications.scss

/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*/
@import "bootstrap";
body {
    padding-top: 70px;
  }

app/assets/javascripts/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, vendor/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.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

【问题讨论】:

    标签: ruby-on-rails webpack ruby-on-rails-6


    【解决方案1】:

    由于 Rails 6 遵循 webpacker,您不需要安装 gem,而是这样做

    在你的 Rails 主目录中,运行这个命令,安装 jQuerypopper.jsbootstrap,jq​​uery 和 popper.js 是 bootstrap 的依赖项。

    yarn add bootstrap@4.4.1 jquery popper.js
    

    然后将其添加到您的 config/webpack/environment.js

    const { environment } = require('@rails/webpacker') // already present
    
    const webpack = require('webpack')
    environment.plugins.append('Provide', new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      Popper: ['popper.js', 'default']
    }))
    
    module.exports = environment // already present
    

    然后在app/javascript/packs内创建一个名为src的文件夹,然后在新创建的src文件夹内创建application.scss文件

    在新创建的application.scss文件中添加这一行

    @import '~bootstrap/scss/bootstrap';
    

    最后在app/javascript/packs/application.js 文件中,添加这个

    import 'bootstrap'
    import './src/application.scss'
    

    重启你的服务器,Bootstrap 已经安装并且应该可以运行了!

    有关 Webpacker 的更多信息 - https://prathamesh.tech/2019/08/26/understanding-webpacker-in-rails-6/

    希望有帮助!

    【讨论】:

    • 我已经运行了yarn add ... 命令,似乎运行良好,但是我在哪里可以找到environments.js 文件?虽然,在此之后我已经完成了其他部分,并且 Bootstrap 似乎一切正常。 environment.js 有什么作用?
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2019-01-12
    • 2013-12-13
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多