自定义 CSS 无法与 Rails 7 的 CSS 捆绑一起使用
在使用 Rails 7 时,我不明白为什么我的自定义 CSS 不起作用。
我使用 Bootstrap 标志构建了新的 Rails 应用程序,该应用程序运行良好(CSS 和 JS,使用 Bootstrap 模式进行了测试)。这些是我的默认配置文件:
application.js
// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"
application.bootstrap.scss
@import 'bootstrap/scss/bootstrap';
package.json
{
"name": "app",
"private": "true",
"dependencies": {
"@hotwired/stimulus": "^3.0.1",
"@hotwired/turbo-rails": "^7.1.0",
"@popperjs/core": "^2.11.2",
"bootstrap": "^5.1.3",
"esbuild": "^0.14.23",
"jquery": "^3.6.0",
"popper.js": "^1.16.1",
"sass": "^1.49.9",
"stimulus": "^3.0.1"
},
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
"build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
}
}
我可以在 /builds 中构建 CSS /application.css
现在我想添加自定义 CSS。这是我的过程:
- 添加了新文件
stylesheets/custom.css
,其中包含 css:
.my-class {
color: #fff;
background-color: #00eb00;
}
添加导入到 application.bootstrap.scss
@import“自定义”;
yarn run build:css
现在我可以在
builds/application.css
中看到.my-class
但是当我尝试在 HTML 中使用 id 时,没有添加 CSS。为什么?我应该把它放在其他地方吗?
编辑:我让它运行,但只有当我手动运行rails asset:precompile然后bin/dev时。 为什么每次更改内容时都需要预编译?
Playing around with Rails 7 and I don't understand why my custom CSS is not working.
I built new rails app with flag for Bootstrap, which is working fine (CSS and JS, tested with bootstrap modal). These are my default config files:
application.js
// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"
application.bootstrap.scss
@import 'bootstrap/scss/bootstrap';
package.json
{
"name": "app",
"private": "true",
"dependencies": {
"@hotwired/stimulus": "^3.0.1",
"@hotwired/turbo-rails": "^7.1.0",
"@popperjs/core": "^2.11.2",
"bootstrap": "^5.1.3",
"esbuild": "^0.14.23",
"jquery": "^3.6.0",
"popper.js": "^1.16.1",
"sass": "^1.49.9",
"stimulus": "^3.0.1"
},
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
"build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
}
}
And I can built CSS in /builds/application.css
Now I want to add custom CSS. This is my process:
- Added new file
stylesheets/custom.css
, with css:
.my-class {
color: #fff;
background-color: #00eb00;
}
Add import to application.bootstrap.scss
@import "custom";
yarn run build:css
And now I can see
.my-class
inbuilds/application.css
But when I try to use id in HTML, no CSS is added. Why? Should I place it somewhere else?
EDIT: I got it running, but only when I run manually rails assets:precompile and then bin/dev.
Why do I need to precompile every time I change something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在新的 Rails 7 应用程序(使用
css=bootstrap
)中,我执行以下操作来获取自定义 css 样式:gem 'sass-rails
,然后捆绑安装 (有关该的更多信息 此处)启动服务器使用
bin/dev
而不是rails server
一切都应该正常(一个很好的测试是将其包含在您的 example.css.scss 文件中,并且 H1s 应该变绿)
资源
一个单独的问题
我也遇到了这个错误(当我用谷歌搜索时,我在这里找到了自己的答案!):
指向这一行:
在这种情况下,运行
yarn run build:css
是解决方案。当我尝试时,我看到:所以我运行了 npm install sass ,然后再次尝试了yarn run build:css ,它成功了。
In a fresh rails 7 app (with
css=bootstrap
), here's what I did to get custom css styles:gem 'sass-rails
in Gemfile, then bundle install (more info on that here)Start your server with
bin/dev
instead ofrails server
Everything should work (a nice test is to include this in your example.css.scss file and H1s should turn green)
Resource
A separate problem
I also got this error (when I googled it, I arrived at my own answer here!):
pointing to this line:
In this case, running
yarn run build:css
was the solution. When I tried that I saw:so I ran
npm install sass
, then triedyarn run build:css
again and it worked.我刚刚遇到这个问题,注意到 JavaScript 和 CSS 在更改后都没有重新编译。
如果您的 gemfile 中包含
gem jsbundling-rails
,请查看 https:// github.com/rails/jsbundling-rails 了解更多详细信息和操作方法。运行
yarn run build:css
重新编译 CSS 资源。我更喜欢使用
./bin/dev
启动本地服务器并监视 JavaScript 和 CSS 更新。I just encountered this problem and noticed both JavaScript and CSS are not recompiled after changes.
If you have
gem jsbundling-rails
included in your gemfile, check out https://github.com/rails/jsbundling-rails for more details and how-tos.Run
yarn run build:css
to recompile the CSS assets.I prefer using
./bin/dev
to start my local server and monitor both JavaScript and CSS updates.yarn run build:css
重新编译 CSS 资源。将本地服务器运行到
./bin/dev
以自动构建 CSS 和 JavaScript 更新。yarn run build:css
to recompile the CSS assets.Run the local server to
./bin/dev
to automatically build the CSS and JavaScript updates.更喜欢使用 foreman 运行应用程序,使用命令 $ bin/dev ,这样它会自动编译 scss 文件。
Prefer running the app using foreman, with command
$ bin/dev
this way it automatically compiles the scss files.它对我有用
如果我添加到 app/assets/config/manifest.js
并且我没有添加@import“custom”, ;到 application.bootstrap.scss
It worked for me if I added
to app/assets/config/manifest.js
And I didn't add @import "custom"; to application.bootstrap.scss