gpt4 book ai didi

ruby-on-rails - 如何让 Bootstrap Javascript 在 Ruby on Rails 6 中工作

转载 作者:行者123 更新时间:2023-12-03 21:16:12 28 4
gpt4 key购买 nike

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

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

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

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

gemfile

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]

应用程序/ Assets /样式表/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;
}

应用程序/ 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 .

最佳答案

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

在您的 Rails 主目录中,运行此命令以安装 jQuery , popper.jsbootstrap , jquery 和 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

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

在新创建的 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/

希望有帮助!

关于ruby-on-rails - 如何让 Bootstrap Javascript 在 Ruby on Rails 6 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60463555/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com