自定义 CSS 无法与 Rails 7 的 CSS 捆绑一起使用

发布于 2025-01-10 15:26:52 字数 1756 浏览 3 评论 0原文

在使用 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。这是我的过程:

  1. 添加了新文件 stylesheets/custom.css,其中包含 css:
    .my-class {
      color: #fff;
      background-color: #00eb00;
    }
  1. 添加导入到 application.bootstrap.scss

    @import“自定义”;

  2. yarn run build:css

  3. 现在我可以在 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:

  1. Added new file stylesheets/custom.css, with css:
    .my-class {
      color: #fff;
      background-color: #00eb00;
    }
  1. Add import to application.bootstrap.scss

    @import "custom";

  2. yarn run build:css

  3. And now I can see .my-class in builds/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

揪着可爱 2025-01-17 15:26:52

在新的 Rails 7 应用程序(使用 css=bootstrap)中,我执行以下操作来获取自定义 css 样式:

  1. 在 Gemfile 中取消注释 gem 'sass-rails,然后捆绑安装 (有关该的更多信息 此处
  2. 创建一个新的CSS文件 app/assets/stylesheets 并将其命名为 example.css.scss
  3. 将此行添加到 app/assets/config/manifest.js:
//= link example.css
  1. 无论您需要访问这些样式,请包含此标签
<%= stylesheet_link_tag "example", "data-turbo-track": "reload" %>
  1. 启动服务器使用 bin/dev 而不是 rails server

  2. 一切都应该正常(一个很好的测试是将其包含在您的 example.css.scss 文件中,并且 H1s 应该变绿)

h1 {
  color: green;
}

资源


一个单独的问题

我也遇到了这个错误(当我用谷歌搜索时,我在这里找到了自己的答案!):

The asset 'application.css' was not found in the load path.

指向这一行:

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

在这种情况下,运行 yarn run build:css 是解决方案。当我尝试时,我看到:

yarn run build:css
rbenv: sass: command not found

The `sass' command exists in these Ruby versions:
  2.7.7
  3.0.3
  3.2.2

所以我运行了 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:

  1. Uncomment gem 'sass-rails in Gemfile, then bundle install (more info on that here)
  2. Create a new css file in app/assets/stylesheets and name it example.css.scss
  3. Add this line to app/assets/config/manifest.js:
//= link example.css
  1. Wherever you need access to those styles, include this tag
<%= stylesheet_link_tag "example", "data-turbo-track": "reload" %>
  1. Start your server with bin/dev instead of rails server

  2. Everything should work (a nice test is to include this in your example.css.scss file and H1s should turn green)

h1 {
  color: green;
}

Resource

  • Very helpful video here

A separate problem

I also got this error (when I googled it, I arrived at my own answer here!):

The asset 'application.css' was not found in the load path.

pointing to this line:

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

In this case, running yarn run build:css was the solution. When I tried that I saw:

yarn run build:css
rbenv: sass: command not found

The `sass' command exists in these Ruby versions:
  2.7.7
  3.0.3
  3.2.2

so I ran npm install sass, then tried yarn run build:css again and it worked.

走野 2025-01-17 15:26:52

我刚刚遇到这个问题,注意到 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.

灵芸 2025-01-17 15:26:52
  1. 添加新文件 stylesheets/custom.css 并创建新的 css 类。
  2. 将导入添加到 application.bootstrap.scss @import "custom";
  3. 然后运行 ​​yarn run build:css 重新编译 CSS 资源。
  4. 现在,您可以在 builds/application.css 中看到自定义类以及 bootstrap css

将本地服务器运行到 ./bin/dev 以自动构建 CSS 和 JavaScript 更新。

  1. Add new file stylesheets/custom.css and create new css class.
  2. Add import to application.bootstrap.scss @import "custom";
  3. then run yarn run build:css to recompile the CSS assets.
  4. Now you can see custom-class in builds/application.css alongside with bootstrap css

Run the local server to ./bin/dev to automatically build the CSS and JavaScript updates.

回眸一遍 2025-01-17 15:26:52

更喜欢使用 foreman 运行应用程序,使用命令 $ bin/dev ,这样它会自动编译 scss 文件。

Prefer running the app using foreman, with command $ bin/dev this way it automatically compiles the scss files.

路弥 2025-01-17 15:26:52

它对我有用

//= link custom.css

如果我添加到 app/assets/config/manifest.js

并且我没有添加@import“custom”, ;到 application.bootstrap.scss

It worked for me if I added

//= link custom.css

to app/assets/config/manifest.js

And I didn't add @import "custom"; to application.bootstrap.scss

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文